Find Even Numbers In An Array In JavaScript

To find even numbers in an array in JavaScript –

  1. Use the filter() method on the array, passing it a function.
  2. This function will take in each element of the array as a parameter (which we have named “num”) and return true if the number is even, or false otherwise.
  3. The numbers that return true when passed into the function will be added to a new array.

Let’s discuss this method in detail below.

find even numbers in an array in JavaScript

Find Even Numbers In An Array In JavaScript

The Array.filter() method is used to create a new array from an existing array, selecting only those elements that pass a certain test specified in a callback function.

For our purposes, we can use this method to find even numbers in an array by passing a function that checks if the number passed into it is even or not.

This function will return true if the number is even, and false otherwise.

The Array.filter() method will then take these Boolean values and create a new array, adding only those elements that returned true.

Let’s see this in action with an example:

var arr = [-2,1,4,2,5,6,3,7];

var evenNumbers = arr.filter(isEven);

function isEven(num){

   return num % 2 === 0;

}

console.log(evenNumbers); //[-2,4,2,6]

In the code above, we have an array called arr which contains both odd and even numbers.

We use the Array.filter() method on this array, passing in a function called isEven().

This function checks if the number passed into it is even by checking if it is divisible by 2 with no remainder.

If it is even, the function returns true, and false otherwise.

The Array.filter() method then creates a new array from the existing one, adding only those elements that returned true when passed into the function.

Thus, the new array evenNumbers contains only even numbers from the original array.

We can also use arrow functions to make our code more concise:

var arr = [-2,1,4,2,5,6,3,7];

var evenNumbers = arr.filter(num => num % 2 === 0);

console.log(evenNumbers); //[-2,4,2,6]

As you can see from the code above, arrow functions allow us to write shorter and more concise code.

Instead of having to write a separate function isEven() and passing it into Array.filter(), we can use an arrow function directly.

This arrow function checks if the number passed into it is even or not, and returns true or false accordingly.

The Array.filter() method then creates a new array from the existing one, adding only those elements that returned true when passed into the function.

Thus, the new array evenNumbers contains only even numbers from the original array.

You can also use the Array.forEach() method to find even numbers in an array.

The forEach() method executes a provided function once for each array element.

We can use this method to check if each number in the array is even, and add it to a new array if it is.

Let’s see this with an example:

var arr = [-2,1,4,2,5,6,3,7];

var evenNumbers = [];

arr.forEach(num => {

  if(num % 2 === 0){

     evenNumbers.push(num);

  }

});

console.log(evenNumbers); //[-2,4,2,6]

In the code above, we have an array called arr which contains both odd and even numbers.

We use the Array.forEach() method on this array, passing in a function that checks if the number passed into it is even or not.

If it is even, the number is added to a new array called evenNumbers.

Thus, the new array evenNumbers contains only even numbers from the original array.

You can also use the Array.reduce() method to find even numbers in an array.

The reduce() method executes a provided function for each value of the array (from left-to-right).

The return value of the function is stored in an accumulator.

We can use this method to check if each number in the array is even, and add it to a new array if it is.

Let’s see this with an example:

var arr = [-2,1,4,2,5,6,3,7];

var evenNumbers = arr.reduce((acc, num) => {

if(num % 2 === 0){

   acc.push(num);

}

return acc;

}, []);

console.log(evenNumbers); //[-2,4,2,6]

In the code above, we have an array called arr which contains both odd and even numbers.

We use the Array.reduce() method on this array, passing in a function that checks if the number passed into it is even or not.

If it is even, the number is added to a new array called acc.

The reduce() method then stores the return value of the function (acc) in a new array called evenNumbers.

Thus, the new array evenNumbers contains only even numbers from the original array.

Conclusion – Find Even Numbers In An Array In JavaScript

In this article, we saw how to find even numbers in an array in JavaScript using different methods in JavaScript.

We can use the Array.filter() method, the Array.forEach() method, or the Array.reduce() method to achieve this.

All of these methods are quite simple and easy to understand and use. I hope this article was helpful to you.

Thanks for reading!

Leave a Reply