Sub topic 9
Exiting and Skipping: break, continue, and Labels
This chapter assumes you know the classic for loop and while loops from the previous chapters, and that you can read if statements and switch. A loop normally runs until its condition becomes false. The break and continue statements let you change that from inside the loop body.
break: leave the loop now
break ends the innermost loop that contains it. Execution continues with the first statement after that loop. Use it when you have found what you were looking for and more iterations would waste work.
const readings = [4, 7, -2, 9, -5];
let firstNegative;
for (let i = 0; i < readings.length; i++) {
if (readings[i] < 0) {
firstNegative = readings[i];
break; // stop scanning; -5 is never examined
}
}
console.log(firstNegative); // -2break works the same way in while and do...while loops. A common pattern is while (true) with a break at the point where the exit condition becomes known.
continue: skip to the next iteration
continue ends only the current iteration. In a for loop, control jumps to the update expression (i++) and then rechecks the condition. In a while loop, it jumps straight to the condition.
let oddSum = 0;
for (let i = 1; i <= 6; i++) {
if (i % 2 === 0) continue; // skip even numbers
oddSum += i;
}
console.log(oddSum); // 9 (1 + 3 + 5)Labels: targeting an outer loop
A plain break exits only the innermost loop. To leave nested loops at once, put a label (an identifier followed by a colon) before the outer loop. Then write break label or continue label.
outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) continue;
if (i * j === 2) break outer;
console.log(i, j);
}
}
// 0 0
// 0 2
// 1 0When i is 1 and j is 2, the product is 2, so break outer ends both loops. continue outer would instead abandon the inner loop and start the next iteration of the outer loop.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| `break` inside a `switch` inside a loop | Exits only the `switch`, and the loop keeps going | Label the loop and use `break loopName`, or `return` from a function |
| `continue` before a `while` counter update | Infinite loop | Update the counter first |
| `break` inside a `forEach` callback | `SyntaxError`, because the callback is not a loop | Use `for` or `for...of`, or `some`/`find` |
| Deeply nested labeled jumps | Hard-to-follow control flow | Move the nested loops into a function and `return` |
Exercise
What does this code print? outer: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (j > i) continue outer; console.log(i, j); } }
Show answer
It prints 0 0, 1 0, 1 1, 2 0, 2 1, 2 2, one pair per line. Whenever j becomes greater than i, continue outer abandons the inner loop and moves to the next i. Each row therefore stops at j equal to i.
Exercise
A loop over commands contains a switch, and the "quit" case calls break. The loop keeps running after "quit". Why, and how do you fix it?
Show answer
Inside a switch, break exits only the switch, not the surrounding loop. Put a label on the loop (for example, commands: for (...)) and write break commands in the "quit" case. If the loop is in its own function, you can use return instead.