Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 2.6 — break, continue and Labels
JavaScript · Example 2 of 3

Labels: leaving nested loops

The plain break finds the 7 in row 1, leaves the inner loop, and then row 2 is searched too. break search stops the whole search.

break leaves a loop (or switch) immediately, continue skips the rest of the current iteration and moves on to the next one, and a label lets either of them act on an outer loop instead of the innermost one.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Loop over [5, 12, 8, 130, 44] and print the first number greater than 100, then stop.

02

Print every number from 1 to 10 except multiples of 3, using continue.

Explain it without notes

01

What is the difference between break and continue?

02

What does a plain break do inside nested loops, and how do you leave both loops?

03

Why can't you use break inside forEach?

Labels: leaving nested loops

Console

(nothing was printed)

Expected console output

found 7 at row 1 col 1
found 7 at row 2 col 1
checks without label: 7
found 7 at row 1 col 1
checks with label: 5