JavaScript Reference ยท Statements and Declarations
The break statement
Reference for the JavaScript break statement: ending loops and switch cases, labeled break for nested loops and blocks, and where break is not allowed.
The break statement ends the innermost enclosing loop (for, for...in, for...of, while, do...while) or switch, and continues with the code that follows. With a label it can exit any enclosing labeled statement, which is how you leave several nested loops at once.
break works only as a statement directly in those constructs. It cannot cross a function boundary: inside a callback passed to forEach it is a SyntaxError.
for (const item of items) { if (item.done) { break; } }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
break; break label;
| Part | Description |
|---|---|
label |
Optional. An identifier naming an enclosing labeled statement. Must be on the same line as break (a line break after break ends the statement). |
Exceptions: a SyntaxError if break is outside a loop, switch or labeled statement, if the label does not exist, or if the label is across a function boundary.
Usage notes
- Innermost only. A bare
breakaffects only the nearest loop orswitch. Inside aswitchinside a loop, it leaves theswitch, not the loop. - Labeled break can target any labeled statement, not just loops, which is occasionally useful for leaving a block early. Use it sparingly; extracting the nested loops into a function and using
returnis often clearer. - Finally blocks. If the
breakleaves atryblock, itsfinallyblock runs first. - Callbacks.
array.forEach()cannot be stopped withbreak. Usefor...of, orsome()/every()/find(), which stop on their own. - Iterators. Breaking out of
for...ofcloses the iterator by calling itsreturn()method. - Not for
if. Without a label,breakis not allowed in anifblock that is not inside a loop or switch. - Compare
continue, which skips only the rest of the current iteration.
Browser support
Supported in every JavaScript engine.