Starlit Daily

📚 › JavaScript: make pages do things

6. Loops: for and while

A for loop repeats code. In for (let i = 1; i <= 5; i++) the variable i starts at 1, the loop runs while i is at most 5, and i increases by one each round.

A while loop repeats while its condition stays true. Make sure something changes, or the loop never ends.

Show example

for (let i = 1; i <= 3; i++) {
  console.log("Round " + i);
}

✍️ Your turn

Print the numbers 1 to 5, one per line, using a for loop.

← Back to course