JavaScript Code

How to iterate over elements of array using While Loop?


Problem Statement

given an array, write a JavaScript program to iterate over the elements of the array using while loop

Solution

while loop is used to repeatedly run a block of code as long as the loop condition evaluates to true. we can use a variable and increment it during iterations to simulate the indices of given array. the condition can be that the index is less than the length of array

Program

let fruits = ['apple', 'banana', 'cherry' ]

let index = 0;
while (index < fruits.length) {
    console.log(fruits[index]);
    index++;
}



copyright @2022