Iteration
Let's create an array with some car manufacturers:
var cars = ['Audi', 'Ferrari', 'Tesla'];
We can iterate through this array with foreach
, for
and also with for of
.
foreach
cars.forEach(car => {
console.log(car);
});
for
for (let i = 0; i < cars.length; i++) {
const car = cars[i];
console.log(car);
}
for of
for (const car of cars) {
console.log(car);
}
reversed order
We can also iterate in reverse by using the for
loop:
for (let i = cars.length - 1; i >= 0; i--) {
const car = cars[i];
console.log(car);
}