Loop (for each) over an array in JavaScript

Created by T.J. CrowderLinked to 27m issues across 76 teams


In JavaScript, you can use a forEach loop to iterate over an array. For example, if you have an array of numbers called numbers:

let numbers = [1, 2, 3, 4, 5];

You can loop over the array using forEach:

numbers.forEach(function(number) { console.log(number); });

This will log each number in the array to the console:

1 2 3 4 5