Looping Over Arrays

Iterate arrays cleanly with for...of, forEach and map.

There are several tidy ways to loop over an array in modern JavaScript.

const colours = ['red', 'green', 'blue'];

for (const colour of colours) {
  console.log(colour);
}

colours.forEach((colour) => console.log(colour));

const upper = colours.map((c) => c.toUpperCase());

Use for...of or forEach to do something with each item, and map when you want a new transformed array back.