iterate over an array in Javascript
Code snippet on how to iterate over an array in Javascript
for (let i = 0; i < array.length; i++) {
const item = array[i];
console.log(item)
}
This code starts at the beginning of an array and iterates through it one item at a time. The ‘let i = 0’ initializes a counter that will keep track of the array’s length and help us loop over each item in it. The ‘i < array.length’ part is a condition that continues the loop until it’s false; once the last item of the array is reached, the condition will be false and the loop breaks. The ‘const item = array[i]’ part sets item equal to the current item in the array. Finally, the ‘console.log(item)’ part prints the value of the item to the console. This loop will then repeat until all items of the array have been visited.