Learn JavaScript
❯ control_flow.break_continue

break, continue & Labels in JavaScript

$concept

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.

$how it works
1

break exits now

Control jumps past the loop's end the moment break executes — remaining iterations never happen.

2

continue skips ahead

Abandons only the current pass; for loops still run their step expression first.

3

Labels reach outward

outer: for (...) { for (...) { break outer; } } exits both loops at once — no flags needed.

4

Prefer over flags

Direct exits beat found-flag plumbing — fewer variables, clearer intent.

$example
let nums = [3, -1, 4, -2, 5];
for (const x of nums) {
if (x < 0) continue;
if (x === 4) break;
console.log(x);
}
Output
3
13 is non-negative and not 4 — prints.
2-1 triggers continue, skipping to 4.
34 triggers break — -2 and 5 never examined.
$use cases
Search loops breaking on found
Filter loops continuing past rejects
Labeled breaks escaping nested loops
Replacing found-flags with direct exits

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.