Find the Even or Odd numbers in an Array in JavaScript

avatar

Borislav Hadzhiev

6 min

banner

Photo from Unsplash

Find the Even or Odd Numbers in an Array in JavaScript #

To find the even or odd numbers in an array:

  1. Use the Array.filter() method to iterate over the array.
  2. Check if each number has a remainder when divided by 2.
  3. The filter method will return a new array containing only the even numbers.
index.js
// โœ… Find the EVEN numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = arr.filter(number => { return number % 2 === 0; }); console.log(even); // ๐Ÿ‘‰๏ธ [2, 4, 6, 8]

The same approach can be used to find the odd numbers in the array.

index.js
// โœ… Find the ODD numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const odds = arr.filter(number => { return number % 2 !== 0; }); console.log(odds); // ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9]

The function we passed to the Array.filter() method gets called with each element in the array.

If the function returns a truthy value, the element is included in the new array.

On each iteration, we use the modulo (%) operator to check if the number doesn't have a remainder when divided by 2.

index.js
console.log(8 % 2); // ๐Ÿ‘‰๏ธ 0 console.log(7 % 2); // ๐Ÿ‘‰๏ธ 1 console.log(6 % 2); // ๐Ÿ‘‰๏ธ 0 console.log(5 % 2); // ๐Ÿ‘‰๏ธ 1

If there is no remainder when the number is divided by 2, it's an even number.

Only even numbers satisfy the condition and get added to the new array.

Conversely, if there is a remainder when dividing by 2, then the number is odd.

If you have to find the even numbers in an array often, define a reusable function.

index.js
function findEvenNumbers(array) { return array.filter(number => { return number % 2 === 0; }); } const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = findEvenNumbers(arr); console.log(even); // ๐Ÿ‘‰๏ธ [ 2, 4, 6, 8 ]

The findEvenNumbers() function takes an array as a parameter and finds all even numbers in the array.

The function can be slightly updated to find the odd numbers in the array.

index.js
function findOddNumbers(array) { return array.filter(number => { return number % 2 !== 0; }); } const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // ๐Ÿ‘‡๏ธ [ 1, 3, 5, 7, 9 ] console.log(findOddNumbers(arr));

The findOddNumbers() function takes an array as a parameter and finds all odd numbers in the array.

An alternative approach is to use the Array.forEach method.

Find the Even or Odd Numbers in an Array using Array.forEach() #

To find the even or odd numbers in an array:

  1. Declare a variable and initialize it to an empty array.
  2. Use the Array.forEach() method to iterate over the array.
  3. Check if each number has a remainder when divided by 2.
  4. If there is no remainder, push the number into the even numbers array.
index.js
// โœ… Find the EVEN numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = []; arr.forEach(number => { if (number % 2 === 0) { even.push(number); } }); console.log(even); // ๐Ÿ‘‰๏ธ [2, 4, 6, 8]

The same approach can be used to find the odd numbers in the array.

index.js
// โœ… Find the ODD numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const odds = []; arr.forEach(number => { if (number % 2 !== 0) { odds.push(number); } }); console.log(odds); // ๐Ÿ‘‰๏ธ [1, 3, 5, 7, 9]

The function we passed to the Array.forEach() method gets called with each element in the array.

However, the forEach() method doesn't return an array like filter() does.

The forEach method returns undefined, so we have to create a new array to store the results.

On each iteration, we check if the current number doesn't have a remainder when divided by 2.

If the condition is met, we push the number into the even numbers array.

Once the forEach() method has iterated over the entire array, the even variable stores all even numbers from the original array.

If you have to do this often, define a reusable function.

index.js
function findEvenNumbers(array) { const even = []; array.forEach(number => { if (number % 2 === 0) { even.push(number); } }); return even; } const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = findEvenNumbers(arr); console.log(even); // ๐Ÿ‘‰๏ธ [ 2, 4, 6, 8 ]

The findEvenNumbers() function takes an array as a parameter and returns the even numbers in the array.

You can slightly tweak the function to find all odd numbers in the array.

index.js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; function findOddNumbers(array) { const onlyOdd = []; array.forEach(number => { if (number % 2 !== 0) { onlyOdd.push(number); } }); return onlyOdd; } const result = findOddNumbers(arr); // ๐Ÿ‘‡๏ธ [ 1, 3, 5, 7, 9 ] console.log(result);

The findOddNumbers() function takes an array as a parameter and returns the odd numbers in the array.

You can also use a for...of loop to find the even numbers in an array.

Find the Even or Odd Numbers in an Array using for...of loop #

To find the even numbers in an array:

  1. Use a for...of loop to iterate over the array.
  2. Check if each number has a remainder when divided by 2.
  3. If the condition is met, push the number into a new array.
index.js
// โœ… Find the EVEN numbers in an array const even = []; const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; for (const number of arr) { if (number % 2 === 0) { even.push(number); } } // ๐Ÿ‘‡๏ธ [ 2, 4, 6, 8 ] console.log(even);

The same approach can be used to find the odd numbers in the array.

index.js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const oddNumbers = []; for (const number of arr) { if (number % 2 !== 0) { oddNumbers.push(number); } } // ๐Ÿ‘‡๏ธ [ 1, 3, 5, 7, 9 ] console.log(oddNumbers);

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

On each iteration, we check if the current number doesn't have a remainder when divided by 2.

index.js
console.log(4 % 2); // ๐Ÿ‘‰๏ธ 0 console.log(3 % 2); // ๐Ÿ‘‰๏ธ 1 console.log(2 % 2); // ๐Ÿ‘‰๏ธ 0 console.log(1 % 2); // ๐Ÿ‘‰๏ธ 1

If the number is even, we push it into the even array.

You can also extract the logic into a reusable function.

index.js
function findEvenNumbers(array) { const onlyEven = []; for (const number of array) { if (number % 2 === 0) { onlyEven.push(number); } } return onlyEven; } const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = findEvenNumbers(arr); console.log(even); // ๐Ÿ‘‰๏ธ [ 2, 4, 6, 8 ]

The function takes an array as a parameter and returns a new array containing only the even numbers of the original array.

The function can be slightly tweaked to return the odd numbers in the array.

index.js
function findOddNumbers(array) { const oddNumbers = []; for (const number of array) { if (number % 2 !== 0) { oddNumbers.push(number); } } return oddNumbers; } const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const odd = findOddNumbers(arr); // ๐Ÿ‘‡๏ธ [ 1, 3, 5, 7, 9 ] console.log(odd);

The function takes an array as a parameter and returns a new array containing only the odd numbers of the original array.

You can also use a basic for loop to find the even numbers in an array.

Find the even or odd numbers in an Array using a for loop #

To find the even numbers in an array:

  1. Use a for loop to iterate over the array.
  2. Access each item using the index.
  3. If the number is even, push it into a new array.
index.js
// โœ… find the EVEN numbers in an array const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const even = []; console.log(even); // ๐Ÿ‘‰๏ธ [ 2, 4, 6, 8 ] for (let index = 0; index < arr.length; index++) { if (arr[index] % 2 === 0) { even.push(arr[index]); } } // ๐Ÿ‘‡๏ธ [ 2, 4, 6, 8 ] console.log(even);

We used a basic for loop to iterate over the array.

On each iteration, we access the array element at the current index and check if it is an even number.

If the condition is met, we push the element into the new array.

The same approach can be used to find the odd numbers in the array.

index.js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const odd = []; for (let index = 0; index < arr.length; index++) { if (arr[index] % 2 !== 0) { odd.push(arr[index]); } } // ๐Ÿ‘‡๏ธ [ 1, 3, 5, 7, 9 ] console.log(odd);

Which approach you pick is a matter of personal preference. I'd use the Array.forEach() method as I find it quite direct and easy to read.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev