Back to If / Then Challenge
Code Studio · Level 4

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.

15-20 min Ages 9-12 Real Code JavaScript

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.

i <= 5 ? keep going? say(...) i++ add 1 false → done!
  1. 1

    The three instructions in the parentheses

    for (let i = 1; i <= 5; i++) packs three jobs separated by semicolons: let i = 1 means start counting at 1, i <= 5 means keep going while i is at most 5, and i++ means add 1 after every lap.

  2. 2

    The counter changes every lap

    The code in the braces runs once per lap, and i is a real variable box you can use inside. That's why say("Jump number " + i) prints "Jump number 1", then "Jump number 2", all the way to 5 — same code, new counter value each time.

  3. 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: change 5 to 8 and run.

Code Editor

Repeat with a loop.

Example code
for (let i = 1; i <= 5; i++) {
  say("Jump number " + i);
}
Output

Program result

Robot ready to speak your strings

Type real JavaScript, then run it.