Last updated: Mar 3, 2024
Reading timeยท6 min
To find the even or odd numbers in an array:
Array.filter()
method to iterate over the array.filter
method will return a new array containing only the even numbers.// โ 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.
// โ 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.
On each iteration, we use the
modulo (%)
operator to check if the number doesn't have a remainder when divided by 2
.
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.
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.
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.
This is a four-step process:
Array.forEach()
method to iterate over the array.even
numbers array.// โ 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.
// โ 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.
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.
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.
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.
for...of
loopThis is a three-step process:
for...of
loop to iterate over the array.2
.// โ 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.
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
.
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.
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.
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.
for
loopThis is a three-step process:
for
loop to iterate over the array.// โ 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.
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.
You can learn more about the related topics by checking out the following tutorials: