JavaScript program to print even numbers in an array
Last Updated :
17 Jun, 2024
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]
- 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.
Example: In this example, we are Using for Loop
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}`);
OutputEven numbers in an array are: 10,12
- 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.
Example: In this example, we are Using while Loop
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}`)
OutputEven numbers in an array are: 44,26,48,64
- 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.
Example: In this example, we are Using forEach Loop
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}`);
OutputEven numbers in an array are: 86,90,24
- 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.
Example: In this example, we are Using filter Method
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}`);
OutputEven numbers in an array are: 86,90,24
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: In this example, we are Using for...of Loop
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);
Method 6: Using reduce
In this approach we use reduce method to iterate over the array and print the event the even numbers.
Example: In this example, we are Using reduce method
JavaScript
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const ans = [];
numbers.reduce(function(acc, number) {
if (number % 2 === 0) {
ans.push(number)
}
return acc;
}, []);
console.log(ans)
Similar Reads
Print all Even Numbers in a Range in JavaScript Array We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in
3 min read
Print all Odd Numbers in a Range in JavaScript Array Odd numbers are numbers that cannot be divided into equal parts. If we divide the odd number by 2, the remainder is 1. In JavaScript, if we want to print all Odd numbers in a range, we can print it by iterating over the array, applying the condition, and printing the odd numbers. There are various a
4 min read
JavaScript Program to Check if a Number is Odd or Even We are going to learn how to check whether the number is even or Odd using JavaScript. In the number system, any natural number that can be expressed in the form of (2n + 1) is called an odd number, and if the number can be expressed in the form of 2n is called an even number.Even = {2k : k â Z}(whe
3 min read
Javascript Program to Print matrix in snake pattern Given n x n matrix In the given matrix, you have to print the elements of the matrix in the snake pattern.Examples: Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {4, 5
2 min read
Javascript Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6
2 min read
Program to print product of even and odd indexed elements in an Array Given an array of integers. The task is to write a program to find the product of elements at even and odd index positions separately. Note: 0-based indexing is considered for the array. That is the index of the first element in the array is zero. Examples: Input : arr = {1, 2, 3, 4, 5, 6} Output :
9 min read
Javascript Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
7 min read
Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space) Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4
5 min read
Menu driven program in C++ to perform various basic operations on array Prerequisite: Switch Statement in C/C++, Functions in C/C++, Loops in C and C++, C/C++ do-while loop with Examples Write a menu-driven program to perform below various basic operations in the array: Print all the even values in the array.Print all the odd values in the array.Sum & average of ele
5 min read
Reverse an Array in Java Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.Let us first see the most common
4 min read