Skip to content
dreamcode
dreamcode
Map
Loops
Lesson 12 of 48
+15 XP on finish
JAVASCRIPT CLIMBSChapter 3 · JS Collections & Loops

Repeating code

In JavaScript, you can repeat code using a for loop. The loop initialization, condition, and increment go inside parentheses, separated by semicolons.

Worked example

How it reads

  • let i = 0 initializes a counter variable at 0.
  • i < 3 keeps looping as long as the counter is less than 3.
  • i++ adds 1 to the counter at the end of each turn.
Cloud tip: You can also loop over arrays using the modern for (const item of array) syntax.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Change the loop so it counts from 10 down to 0 in steps of 2: 10, 8, 6, 4, 2, 0.

Press Run to check your work.