JavaScript Simplified · One concept, one day, one project · Day 8 of 32
Loops
Repeating work without repeating yourself. You learn the for loop and the while loop, then generate the numbers 1 to 20 automatically.
By the end of today you can
- Repeat a block a set number of times with a
forloop - Repeat while a condition holds with a
whileloop - Read the three parts of a for loop (start, condition, step)
- Build a number generator
Imagine printing the numbers 1 to 20 by writing twenty console.log lines. Nobody does that. A loop repeats a block of code for you. It is one of the biggest jumps in power you will make, and it closes out the "make JavaScript think" layer.
1. The for Loop
Definition
A for loop repeats a block a known number of times. It has three parts inside its brackets, separated by semicolons: where to start, the condition to keep going, and the step that happens after each round.
let i = 1 starts the counter. i <= 5 keeps the loop running while true. i++ adds 1 after each round. The block runs five times, with i as 1, 2, 3, 4, 5.
i++ is shorthand for "add one to i". The counter i is just a variable; it is traditionally called i, for index.
2. The while Loop
Definition
A while loop repeats as long as a condition is true. Use it when you do not know in advance how many times you will loop, only the condition to stop on.
count > 0. Each round prints and then lowers count, so it reaches 0 and stops. The line that changes count is what ends the loop.
If a while loop never changes the thing it is checking, it runs forever and freezes the page. Every while loop needs a line that moves it closer to stopping, like count = count - 1.
3. Loop Control: break and continue
break stops a loop early. continue skips the rest of the current round and jumps to the next one.
continue), prints 5, 6, then stops at 7 (break). Two handy ways to steer a loop.Today’s Mini Project
🔢 Number Generator
Print the numbers 1 to 20 with a single loop. Then, as a small twist, mark the even ones, using the remainder operator you met on Day 4.
i % 2 === 0 marks the even ones. Three of this week’s ideas working together: a loop, a condition and a remainder.Change the loop so it prints only the even numbers from 1 to 20, and nothing else. Expected: 2, 4, 6, ... 20, one per line.
Write a for loop that prints your name five times.
Expected: your name, five times. Then change the 5 to 3 and watch it print three times.
You have finished the first layer. JavaScript can now store values, make decisions and repeat work: it can think. Next week you learn to organise that thinking into reusable pieces, so your programs stop being one long list and start being built from parts.
Checkpoint
What are the three parts inside a for loop’s brackets?
The start (let i = 1), the condition to keep going (i <= 5), and the step after each round (i++). Together they control how many times the block runs.
When would you use a while loop instead of a for loop?
When you do not know how many times you will repeat, only the condition to stop on. A for loop suits a known count; a while loop suits "keep going until this changes".
What is the danger with a while loop?
If nothing moves it towards its stop condition, it runs forever and freezes the page. Always include a line that brings the loop closer to stopping.
Tick Before You Move On
- ☐ I can repeat a block a set number of times with a for loop
- ☐ I can repeat while a condition holds with a while loop
- ☐ I can steer a loop with break and continue
- ☐ I built the Number Generator
Quick Recap
A loop repeats a block so you do not repeat yourself. A for loop has start, condition and step, and suits a known count. A while loop runs as long as its condition is true. break stops a loop early; continue skips to the next round; always move towards stopping.
Next: Day 9, and a new layer: making JavaScript reusable. You start with functions, named blocks of code you can run again and again.