javascript · level 1 · unit 5 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
stops a loop early. skips a turn.
Picture a lift going up a tall building. You ride past each floor, and when you reach yours, you get out. You do not ride to the top. break is getting out: the loop stops straight away.
Some buildings have no floor 13, so the lift skips it and carries on. continue is that skip: JavaScript drops the rest of this turn and jumps to the next one.
Both usually sit inside an if, so they only happen when its condition is true. Any lines after them in the same turn do not run.
for (let floor = 10; floor <= 15; floor++) {
if (floor === 13) { continue; }
if (floor === 15) { break; }
console.log("Ding! Floor", floor);
} Like every instruction, break and continue end with a semicolon.
guess first
Song 2 gets skipped. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
for (let song = 1; song <= 4; song++) {
if (song === 2) { continue; }
console.log("Playing song", song);
}
your turn
Your turn. Skip the unlucky number.
Count from 1, but skip 4 and stop the loop when it reaches 8. Fill in the two numbers, then press run.
fix it
One cookie is broken, not the whole box. Fix it!
- Cookie 3 is broken, so skip it. But should the whole loop stop there?
breakends the loop. You want the command that only skips one turn.- Change
breaktocontinueon line 2, then press run.
nailed it
you can stop and skip in loops. :D
breakstops the whole loop straight away.continueskips the rest of this turn only.- Put them inside an
ifso they happen at the right time.
new words in your collection
- break
- continue
This lesson isn’t saved. A free account remembers every lesson you finish.