#88: break outer from inside a nested loop
Topic: scope · Difficulty: ★★
outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) break outer;
console.log(i, j);
}
}Output
0 0
Why
A plain `break` only escapes the innermost loop it's inside — here, that would just move on to the next i and keep going. Prefixing a loop with a label (outer:) and writing `break outer` instead tells the engine to stop that specific labeled loop entirely, unwinding out of both loops at once instead of just the inner one. The inner loop runs once with i=0, j=0, logging '0 0', then hits j=1 and breaks all the way out of outer — so i never reaches 1 or 2, and the loop body never runs again.