JavaScript Code


How to iterate over elements of array using For Loop in JavaScript?


Iterate over elements of array using For Loop in JavaScript

In this tutorial, you are given an array of elements. Write a JavaScript program to iterate over the elements of the array using for loop.

Solution

In JavaScript, For 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 For loop statement to iterate over the elements of this array.

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

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



copyright @2022