How to use while Loop In Javascript
- 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}`) |
Output
Even numbers in an array are: 44,26,48,64
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]