JavaScript Labeled Statements: break and continue Outer Loops
JavaScript labeled statements let break and continue target an outer loop or block by name. Syntax, real use cases, gotchas, and cleaner alternatives.
A labeled statement in JavaScript attaches a name to a loop or block so that break and continue can target it directly. Normally, break exits only the innermost loop and continue skips to the next iteration of the innermost loop. With a label, you can break out of an outer loop from deep inside a nested one, or continue the outer loop without finishing the inner one. It’s a small, old, and rarely used part of the language — and occasionally exactly the right tool.
The syntax
A label is an identifier followed by a colon, placed directly before a statement:
outer: for (const row of grid) {
for (const cell of row) {
if (cell === target) {
break outer;
}
}
}
break outer exits both loops at once. Without the label, break would only end the inner for...of, and the outer loop would move on to the next row.
continue works the same way:
rows: for (const row of rows) {
for (const value of row) {
if (value < 0) {
continue rows; // skip the rest of this row
}
}
processRow(row);
}
Here, finding a negative value abandons the current row entirely and moves on to the next one. processRow only runs for rows with no negatives.
A few rules apply:
- Labels can’t be reserved words, and in strict mode
let,yield, and similar names are off-limits too. - A label is only visible inside the statement it labels. You can’t jump to a label elsewhere in the function; there’s no
goto. continue labelrequires the label to be on a loop.break labelworks on any labeled statement, including a plain block.- Labels don’t cross function boundaries. A
break outerinside a callback passed toforEachis a syntax error, because the callback is a separate function.
Breaking out of a labeled block
The least-known form is a label on an ordinary block:
validate: {
if (!user) break validate;
if (!user.email) break validate;
if (!user.emailVerified) break validate;
grantAccess(user);
}
break validate jumps to the end of the block, skipping the remaining statements. It behaves like an early return without needing a function. It’s legal and occasionally handy in generated code, but in hand-written code most people will find an early return from a small function easier to read.
When labels are genuinely useful
Labels earn their place in a few patterns.
Searching nested structures. Finding the first match in a 2D grid, a matrix, or nested arrays is the textbook case. Once you’ve found what you need, scanning the remaining rows is wasted work, and break outer stops all of it.
Skipping an outer iteration based on an inner check. Validating each record against several rules and abandoning the record on the first failure is a natural fit for continue outer.
Performance-sensitive loops. In hot paths, plain for loops with labels avoid the closure allocation and per-item function calls of array methods. That matters less than people assume for most code, but in tight numeric loops — parsing, games, data processing — it can be the clearest way to write the control flow. If you’re working in this territory, see how to break up long tasks in JavaScript so a long loop doesn’t block the main thread.
Generated code. Compilers and transpilers sometimes emit labeled blocks because they’re a straightforward way to express structured jumps.
Gotchas and readability concerns
They’re unfamiliar. Many developers have never seen a labeled statement, so outer: at the top of a loop can look like a typo or an object literal key. Use descriptive names — rows, search, outer — and keep the labeled code short.
Labels vs object literals. At the start of a statement, { foo: 1 } is parsed as a block containing a labeled expression statement, not an object. That’s the reason an arrow function returning an object needs parentheses: () => ({ foo: 1 }). Without them, the braces form a function body, foo: is a label, and the function returns undefined.
They don’t work with array methods. forEach, map, some, and friends take callbacks, and labels can’t reach across the function boundary. If you need early exit from a nested iteration, you need real loops or a different structure.
Linters may flag them. Some style guides disallow labels outright, and common lint rules warn about unused labels or labels on non-loop statements. If your codebase bans them, follow the rule; the alternatives below cover most cases.
Cleaner alternatives
Labels are one option among several. Often a different shape of code is clearer.
| Approach | How it exits nested loops | Good for | Trade-off |
|---|---|---|---|
Labeled break / continue | Jumps directly to the named loop | Tight nested loops, hot paths | Unfamiliar syntax |
Extract to a function and return | return leaves every loop at once | Most search logic | Extra function and parameters |
| Flag variable | Inner loop sets a flag; outer loop checks it | Very simple cases | Easy to forget a check |
some / find / every | Callback returning a value stops iteration | Readable searches over arrays | Callback overhead, no labels |
| Generators | Yield matches lazily; consumer stops when done | Composable traversal | More machinery |
Extract and return is usually the most readable choice:
function findCell(grid, target) {
for (const [r, row] of grid.entries()) {
for (const [c, cell] of row.entries()) {
if (cell === target) return [r, c];
}
}
return null;
}
Array methods can express nested searches too. some stops as soon as its callback returns true, and find stops at the first match:
const found = grid.some((row) => row.includes(target));
Generators separate traversal from the decision to stop. A generator that yields every cell lets the caller use for...of with an ordinary break, and the generator’s cleanup runs automatically. See generators and iterators in JavaScript and the newer iterator helpers for that approach.
How labels relate to closures and scope
A label isn’t a variable. It doesn’t hoist, it isn’t captured by closures, and it lives in its own namespace, so a label named count won’t clash with a variable named count. It exists only as a target for break and continue within its own statement. That narrow scope is why labels are safe to use: they can’t create the tangled, jump-anywhere control flow that gave goto its reputation.
The takeaway
Labeled statements let break and continue target an outer loop or block by name, which is the cleanest way to escape nested loops without flags or extra functions. They’re scoped, safe, and well supported, but unfamiliar to many readers. Reach for them in tight nested loops, prefer an extracted function with return when readability matters most, and remember they can’t reach into callbacks passed to array methods.
Tagged
Keep reading
Takina · · 5 min read Declarative Shadow DOM: Server-Rendered Web Components
Declarative Shadow DOM lets HTML create a shadow root with a template element, so web components can render on the server and display before JavaScript.
Takina · · 5 min read JavaScript Async Iterators and for await...of Explained
Async iterators let JavaScript loop over data that arrives over time — paginated APIs, streams, events — with for await...of and async generators.
Takina · · 5 min read Module Federation Explained: Sharing Code at Runtime
Module Federation lets separately built and deployed JavaScript apps load each other's modules at runtime. How hosts, remotes, and shared dependencies work.