How to Separate Even and Odd Numbers in an Array Using JavaScript

How to Separate Even and Odd Numbers in an Array Using JavaScript

Arrays are very important for working with different types of data in JavaScript. Sometimes we need to work with only even or odd numbers from an array as per our needs. For example, we have an array of numbers and we need to separate even and odd numbers from this array. In today's blog, we will discuss in detail how to easily extract even or odd numbers from an array, so let's get started –

 

Problem: Extract even and odd numbers from an array?

 

Code and explanation:


const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const evenNumbers = numbers.filter(num => num % 2 === 0);
const oddNumbers = numbers.filter(num => num % 2 !== 0);

console.log("Even numbers:", evenNumbers);
console.log("Odd numbers:", oddNumbers);


Code explanation:

  • First, an array named numbers is created where some even and odd numbers are kept.
  • Then we create a new array named evenNumbers, where only those numbers are selected using the numbers.filter() function which satisfies the condition num % 2 === 0. Here num % 2 === 0 means that the number is divisible by 2, i.e. even.
  • Similarly, another new array named oddNumbers is created. Here, using the numbers.filter() function, only those numbers are selected that satisfy the condition num % 2 !== 0. Here num % 2 !== 0 means that the number is not divisible by 2, i.e. odd.
  • Finally, using console.log(), even and odd numbers are displayed separately.


Output


Even numbers: [2, 4, 6, 8, 10]
Odd numbers: [1, 3, 5, 7, 9]

 

Method 2 –


const numbers = [11, 22, 33, 44, 55, 66, 77, 88, 99, 100];

let evenNumbers = [];
let oddNumbers = [];

for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] % 2 === 0) {
    evenNumbers.push(numbers[i]);
  } else {
    oddNumbers.push(numbers[i]);
  }
}

console.log("Even numbers:", evenNumbers);
console.log("Odd numbers:", oddNumbers);


Explanation of the code:

  • First, an array named numbers is created in which some numbers are stored.
  • Two empty arrays named evenNumbers and oddNumbers are created, where even and odd numbers will be stored separately.
  • Then, each element of the numbers array is checked using a for loop.
  • Using the condition if (numbers[i] % 2 === 0) we check whether the current number is divisible by 2 or not. If it is divisible, then the number is even and it is added to the evenNumbers array. Otherwise, i.e. it will enter the else part and if the number is odd, it will be added to the oddNumbers array.
  • Finally, using console.log(), even and odd numbers are displayed separately.


    Output

    
    Even numbers: [22, 44, 66, 88, 100]
    Odd numbers: [11, 33, 55, 77, 99]
    

     

    Bookmark the website! If you like the post and don't forget to share the post so that your friends don't miss it. Be sure to like and comment so that we can constantly come up with new problems along with solutions and if you also have any coding-related problems, let us know in the comment box and we will try to solve them, InshaAllah.

    About the author

    Ahshan Habib
    Hello! I am Ahshan Habib. I am a MERN Stack web developer. Blogging is my hobby and I would like to share my knowledge with everyone. Here I will share every day about education, technology, and Programming. So stay with us and share my page on your…

    Post a Comment