Loop through an array in JavaScript

Created by Christian C. SalvadóLinked to 92m issues across 255 teams

tl;dr

In JavaScript, you can loop through an array using a for loop. For example, if you have an array called myArray with the elements [1, 2, 3, 4], you can loop through it like this:

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

This will print out each element of the array, one at a time, to the console. The i variable is used to keep track of the index of the array, and the myArray.length is used to make sure the loop stops when it reaches the end of the array.