JavaScript

How to filter odd number using from array in JavaScript?

How to filter odd number using from array in JavaScript?, someone asked me to explain?

In this article we will discuss implementing how to filter odd number using from array in JavaScript. Below example we are using a callback function Isodd it will result the odd numbers.

Example:

<script type="text/javascript">

    function Isodd(value, index, array) {

        if (value % 2 == 1) {
            return true;
        }
        else {
            return false;
        }
    }

        // Source array
    var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    // Pass the callback function asargument to the filter method

    var result = myArray.filter(Isodd);
   alert(result);
  </script>

 

Output:

filter odd number using from array in JavaScript

Post your comments / questions