Iterate over elements of array using While Loop in JavaScript
In this tutorial, you are given an array. You should 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
In the following program, we take an array of strings in fruits
. And use a While loop statement to iterate over the elements of this array.
let fruits = ['apple', 'banana', 'cherry' ]
let index = 0;
while (index < fruits.length) {
console.log(fruits[index]);
index++;
}