JavaScript: loops

Loops repeat a block of code while a condition holds or while items remain in a collection.

What you will learn

Minimal examples

const colors = ['red', 'green', 'blue'];
for (const color of colors) {
  console.log(color);
}

for (let i = 0; i < 3; i++) {
  console.log(i);
}

Use for...of when you need each value. Use a counted for loop when the index or a precise number of iterations matters.

Common mistakes