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
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
Sliding Window Technique Sliding Window Technique is a method used to solve problems that involve subarray or substring or window. The main idea is to use the results of previous window to do computations for the next window. This technique is commonly used in algorithms like finding subarrays with a specific sum, finding t
13 min read
Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Example of an AVL Tree:The balance factors for different nodes are : 12 :1, 8:1, 18:1, 5:1, 11:0, 17:0 and 4:0. Since all differences
4 min read