Here are different approaches to loop through an array in Javascript.

Whenever you want to loop through an array in Javascript, the common approach taken is using the forwhile, or any of the similar loops. Although this is a valid choice, there are many other approaches that you can take to loop through an array in Javascript.


forEach allows you to loop through all items in an array. For example, a for loop like this:

for (let i = 0; i < arr.length; i++) {

will become:

arr.forEach((item) => console.log(item));

This eliminates the need to keep using the index to access the item, especially when the items in the array are objects and accessing their properties can become a hassle while using the index (unless you assign it to a new variable in the loop.)

#beginners #javascript

All The Ways You Can Loop Arrays in Javascript
1.45 GEEK