How to use for…of Loop In Javascript
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); |
Output
[ 2, 4, 6, 8 ]
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:
Table of Content
- Using for Loop
- Using while Loop
- Using forEach Loop
- Using filter Method
- Using for…of Loop
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]