Real JavaScript Loops
Don't repeat yourself — loop! Write one line and make the robot do it 5, 8, or 100 times. Loops are how games draw whole worlds without writing a million lines.
What is a loop?
A loop tells the computer to repeat something without you copying
the command over and over. It's like telling a friend "do 5 jumping jacks" instead
of shouting "jump!" five separate times. The for loop keeps a
counter, runs your code one lap at a time, and stops when the
counter passes the limit.
-
1
The three instructions in the parentheses
for (let i = 1; i <= 5; i++)packs three jobs separated by semicolons:let i = 1means start counting at 1,i <= 5means keep going while i is at most 5, andi++means add 1 after every lap. -
2
The counter changes every lap
The code in the braces runs once per lap, and
iis a real variable box you can use inside. That's whysay("Jump number " + i)prints "Jump number 1", then "Jump number 2", all the way to 5 — same code, new counter value each time. -
3
Why coders love loops
Want 100 jumps instead of 5? Change one number. Without a loop you'd copy
say(...)a hundred times — and fixing a typo would mean fixing it a hundred times too. Try it: change5to8and run.
Repeat with a loop.
for (let i = 1; i <= 5; i++) {
say("Jump number " + i);
}
Program result
- Run your code to see output.
Type real JavaScript, then run it.
You used JavaScript to repeat a command with a counter.
Open Loop Builder