Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
for (const item of items) {
  if (item.done) {
    break;
  }
}

Try it

JS Leaving loops early

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Labeled break for nested loops

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 break affects only the nearest loop or switch. Inside a switch inside a loop, it leaves the switch, 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 return is often clearer.
  • Finally blocks. If the break leaves a try block, its finally block runs first.
  • Callbacks. array.forEach() cannot be stopped with break. Use for...of, or some() / every() / find(), which stop on their own.
  • Iterators. Breaking out of for...of closes the iterator by calling its return() method.
  • Not for if. Without a label, break is not allowed in an if block 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.