break, continue & Labels in JavaScript
break exits its nearest loop or switch immediately; continue skips to the next iteration (running for-loop steps first). Together they express early exits and skips more readably than flags — search loops break on found, filters continue on rejects.
Labeled break/continue (outer: for... break outer;) extend control to nested loops — JavaScript's structured answer to goto. Labels are rare in idiomatic code but legitimate for breaking out of deep nesting without flag variables.
break exits now
Control jumps past the loop's end the moment break executes — remaining iterations never happen.
continue skips ahead
Abandons only the current pass; for loops still run their step expression first.
Labels reach outward
outer: for (...) { for (...) { break outer; } } exits both loops at once — no flags needed.
Prefer over flags
Direct exits beat found-flag plumbing — fewer variables, clearer intent.
let nums = [3, -1, 4, -2, 5];for (const x of nums) { if (x < 0) continue; if (x === 4) break; console.log(x);}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.