Loop inside javascript ES6

1 month ago23k times

In javascript, there are many ways to iterate through the array. In this example, we will use for...of function.


const alpha = ['ABC', 'EFG','IJK','LMN'];
for (const alphas of alpha) {
    console.log(alpha);
}
Other answers

Here we can use map function to iterate through the array.


const alpha = ['ABC', 'EFG','IJK','LMN'];
alpha.map((values)=>{
  console.log(values);
});

Iterating using the ForEach loop function


const alpha = ['ABC', 'EFG','IJK','LMN'];
alpha.forEach(values => {
    console.log(values) ;
})

Old For loop approach can be used. but not recommended in modern JavaScript ES6. Code is below


const alpha = ['ABC', 'EFG','IJK','LMN'];
 
for (let index = 0; index < alpha.length; ++index) {
    const element = alpha[index];
    console.log(element);
}

Add Answer

Add a codeAdd Code
Remove adsremove