How to find Even Numbers in an Array using JavaScript

← PrevNext →

Many young developers ask this question and there are many simple solutions. You can loop through each number in an array and using a formula, get the even numbers. However, there’s another method, which is more efficient than the for loop or the forEach() method, it is the JavaScript array filter() method.

Find even numbers in an array using JavaScript .filter() method

We’ll see all the three methods here to find even numbers in an array. I have explained each example using ES6 features also.

Find all Even Numbers in an Array using for loop

Using the for loop, you can iterate through each item (numbers in our case) in the array and find the even numbers. For example,

<script>
    function getEvenNumbers() {
        var arr = [1, 2, 3, 4, 5, 6];

        for (var i = 0; i < arr.length; i++) {
            if (arr[i] % 2 === 0) {
                document.writeln(arr[i] + "<br />");
            }
        }
    }

    getEvenNumbers();
</script>
Try it

Using ES6 features, the script should be,

<script>
    let getEvenNumbers = () => {
        const arr = [1, 2, 3, 4, 5, 6];
        
        for (num of arr){	
            if (num % 2 === 0) {       // 0 for even numbers and 1 for odd numbers.
                document.writeln(num + "<br />");
            }
        }
    }

    getEvenNumbers();
</script>

Note: The formula to get all the odd numbers from the array is,

// To get odd numbers.
if (num % 2 === 1) { ... }


You cannot miss this post: How to filter out only numbers in an Array using Pure JavaScript

Get Even Numbers using array.forEach() Method

The JavaScript .forEach() method is more efficient than the traditional for loop method, to iterate an array. The formula to get even numbers from the array is similar to similar to the one you saw in the first method above. The technique is slightly different.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript .forEach() Method</title>
</head>
<body>
    <p>
    	JavaScript forEach() method to get even numbers in an Array.
    </p>
</body> 
<script>
    function getEvenNumbers(num) {
        if (num % 2 === 0) {
            document.writeln(num + "<br />");
        }
    }

    var arr = [1, 2, 3, 4, 5, 6];
    arr.forEach(getEvenNumbers);
</script>
</html>
Try it

Using ES6 features,

let getEvenNumbers = (num) => {
    if (num % 2 === 0) {
        document.writeln(num + "<br />");
    }
}
    
const arr = [1, 2, 3, 4, 5, 6];
arr.forEach(getEvenNumbers);

Find all Even numbers in an Array using .filter() method

The .filter() method is more efficient (Why? read the conclusion below) than both the methods that I have shown in the above examples.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript .filter() Method Example</title>
</head>
<body>
    <p>
    	Using JavaScript Array.filter() method to get Even numbers in an Array.
    </p>
</body> 
<script>	
    function getEvenNumbers() {
        var arr = [4,5,7,8,14,45,76];
    	
        var evens = arr.filter(number => number % 2 == 0);
        document.write('Even Numbers: ' + evens);
    }
    
    getEvenNumbers();
</script>
</html>
Try it

the above script using ES6 features...

let getEvenNumbers = () => {
    let arr = [4,5,7,8,14,45,76];
    	
    let the_evens = arr.filter(number => number % 2 == 0);
    document.write('Even Numbers: ' + the_evens);
}
    
getEvenNumbers();

The Array.filter() method returns a new array of items that has to pass a test function (the value, which can be a number or a string, has to pass a condition). Therefore, if the condition returns true, the item (a number, in our case) is added to the new array.

Conclusion

Now, if you compare the .filter() method with the for loop and forEach(), the .filter() is a one line solution, where the focus is on the test function (the logic here is: number => number % 2 == 0), no need to run a loop explicitly. The method will take care of everything else.

And hey... the formula to get all the odd numbers from the array is,

// To get odd numbers.
if (num % 2 === 1) { ... }

Use 0 for even numbers and 1 for odd numbers.

Well, that’s it. Thanks for reading.

← PreviousNext →


Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.
Delivered by FeedBurner