How to Find Even or Odd Numbers in an Array in JavaScript
Filtering an array to separate its even and odd numbers is a classic programming exercise and a common data manipulation task. The most efficient and readable way to accomplish this in modern JavaScript is by using the Array.prototype.filter() method combined with the modulo operator (%).
This guide will teach you how to use this powerful combination to extract even or odd numbers from any array.
The Core Logic: The Modulo Operator (%)
The key to determining if a number is even or odd is the modulo operator (%), which returns the remainder of a division operation.
- A number is even if it can be divided by 2 with no remainder (
number % 2 === 0). - A number is odd if it has a remainder of 1 when divided by 2 (
number % 2 !== 0).
This simple check is the foundation of our filtering logic.
The Modern Method (Recommended): Using Array.prototype.filter()
The filter() method is the perfect tool for this job. It creates a new array containing only the elements that pass a specific test.
For example, we have an array of numbers and we need to create two new arrays: one with only the even numbers, and one with only the odd numbers.
// Problem: How to separate the even and odd numbers from this array?
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Solution: By providing a simple callback function to filter(), we can create our new arrays in a single, readable line.
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// --- Find the EVEN numbers ---
const evenNumbers = numbers.filter(number => {
return number % 2 === 0;
});
console.log('Even numbers:', evenNumbers); // Output: [2, 4, 6, 8]
// --- Find the ODD numbers ---
const oddNumbers = numbers.filter(number => {
return number % 2 !== 0;
});
console.log('Odd numbers:', oddNumbers); // Output: [1, 3, 5, 7, 9]
This functional approach is concise, declarative, and the recommended best practice in modern JavaScript.
An Alternative: The Traditional for...of Loop
While filter() is more concise, a traditional for...of loop is also a perfectly valid and readable way to accomplish the same goal. This imperative approach can be easier to understand for those new to functional programming.
The logic:
- Create a new, empty array to store the results.
- Loop through each
numberof the original array. - Inside the loop, use an
ifstatement with the modulo operator to check if the number is even or odd. - If it meets the condition,
push()it into the new array.
Solution:
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const evenNumbers = [];
for (const number of numbers) {
if (number % 2 === 0) {
evenNumbers.push(number);
}
}
console.log('Even numbers:', evenNumbers); // Output: [2, 4, 6, 8]
This method is slightly more verbose but achieves the exact same result and is just as performant for this task.
Conclusion
Separating even and odd numbers from an array is a simple task once you understand the modulo operator.
- The core logic for checking if a number is even is
number % 2 === 0. - The
Array.prototype.filter()method provides the most concise and modern solution for creating a new array based on this condition. - A traditional
for...ofloop is a perfectly valid and readable alternative that achieves the same result.
For most modern JavaScript projects, the filter() method is the preferred choice for its functional and declarative style.