freecoding.school100% FREE · NO SIGNUP
Script SpringsISSUE #25 of 29

for loops · classic

SparkyVSNaN the Trickster
Sparky saysThe classic for loop repeats with an init, condition, and step.

for (let i = 0; i < n; i++) gives full control over counting — ideal when you need the index or a non-trivial step. Use let (not var) so each iteration scopes i correctly, especially with closures/timeouts inside the loop.

Power-ups you unlock

NaN the Trickster attacks — common mistakes

Boss battleLoop 0–4 with a for loop and collect the squares into an array.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const sq=[]; for(let i=0;i<5;i++) sq.push(i*i);
document.getElementById('o').textContent=sq.join(' ');</script></body></html>
▶ Open the interactive comic issue
‹ Switch · Case · Break · DefaultFor...In · Iterating Keys ›