JavaScript Code

How to iterate over elements of array using For Loop?


Problem Statement

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

Solution

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

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

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



copyright @2022