Sunday, September 22, 2024
Google search engine
HomeLanguagesJavascriptJavaScript program to print even numbers in an array

JavaScript program to print even numbers in an array

Given an array of numbers and the task is to write a JavaScript program to print all even numbers in that array. We will use the following methods to find even numbers in an array:

Example:

Input:    numbers_array1= [4, 76, 89, 61, 72, 64]
Output: [4,76,72,64]
Input: numbers_array2= [15, 60, 90, 14, 7, 45]
Output: [60,90,14]

Method 1: Using for Loop

Approach: 

  • Iterate each element in the array using for loop to check if (num %2==0), the condition to check even or not. 
  • If the condition is satisfied then push the num into the even list. 
  • After iterating all elements in an array print the even list.

Javascript




// Initializing numbers array
let numbers = [10, 23, 12, 21];
 
// Declaring empty Even array
let even = [];
for(let i = 0; i < numbers.length; i++) {
       if (numbers[i] % 2 == 0)
       even.push(numbers[i]);
}
 
// Printing output
console.log(`Even numbers in an array are: ${even}`);


Output

Even numbers in an array are: 10,12

Method 2: Using while Loop

Approach: 

  • Iterate through the array using the while loop.
  • Check elements if (element%2==0) condition satisfies, we push the element into an even array. 
  • After iterating all elements using a while loop, we print an even numbers array.

Javascript




// Initializing numbers array
let numbers=[44, 26, 48, 64, 27, 53];
 
// Declaring empty Even array
let even = [];
let i = 0;
while(i < numbers.length) {
       if (numbers[i] % 2 == 0)
       even.push(numbers[i]);
       i++;
}
 
// Printing output
console.log(`Even numbers in an array are: ${even}`)


Output

Even numbers in an array are: 44,26,48,64

Method 3: Using forEach Loop

Approach:

  • Declare an array called “numbers” containing a set of integers.
  • Declare an empty array called “even”.
  • Use the forEach method to iterate through each element in the “numbers” array.
  • Within the forEach loop, use an if statement to check if the current element is even using the modulus operator (element%2==0).
  • If the element is even, use the push method to add it to the “even” array.
  • After the forEach loop, use console.log () to display the message “Even numbers in an array are: [even numbers]” where [even numbers] is the array of even numbers.

This approach allows one to iterate through the entire array, check each element for even-ness, and add even numbers to a separate array, which is then displayed in the console.

Javascript




// Initializing numbers array
let numbers = [86, 41, 55, 85, 90, 24];
 
// Declaring empty Even array
let even = [];
numbers.forEach(element => {
    if( element%2 == 0 )
    even.push(element);
});
 
// Printing output
console.log(`Even numbers in an array are: ${even}`);


Output

Even numbers in an array are: 86,90,24

Method 4: Using filter Method

Approach:

  • Declare an array called “numbers” containing a set of integers.
  • Use the filter() method on the “numbers” array, passing in a callback function as the argument.
  • The callback function should take in the current element as a parameter and use the modulus operator (element%2==0) to check if it is even.
  • Return true for even numbers and false for odd numbers in the callback function.
  • Assign the returned result of the filter method to a variable, let’s say ‘evenNumbers’.
  • After the filter method, use console.log() to display the message “Even numbers in an array are: [evenNumbers]” where [evenNumbers] is the array of even numbers returned from the filter method.

The filter method returns a new array with all elements that pass the test implemented by the callback function.

Javascript




// Initializing numbers array
let numbers = [86, 41, 55, 85, 90, 24];
 
let evenNumbers = numbers.filter(function(element) {
      return element % 2 === 0;
});
 
// Printing output
console.log(`Even numbers in an array are: ${evenNumbers}`);


Output

Even numbers in an array are: 86,90,24

Method 5: Using for…of Loop

The for…of loop iterates over the iterable objects (like Array, Map, Set, arguments object, …,etc), invoking a custom iteration hook with statements to be executed for the value of each distinct property.

Example:

Javascript




const even = [];
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for (const num of arr) {
    if (num % 2 === 0) {
        even.push(num);
    }
}
console.log(even);


Output

[ 2, 4, 6, 8 ]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments