Skip to main content

The break keyword

The break keyword in JavaScript is used to stop a loop or a switch block earlier than it would finish naturally. Control immediately moves to the next line after the construct that was interrupted.

Theory

TL;DR

  • break immediately ends the nearest enclosing loop or switch block.
  • In loops it is used to stop iterating once the result has been found.
  • In a switch it prevents execution from falling through into the following case clauses.
  • break may only appear inside a loop or a switch, otherwise you get a SyntaxError.
  • To skip a single iteration instead of ending the loop, use continue.
  • With a label, break label exits the outer loop rather than the inner one.

Quick example

javascript
for (let i = 0; i < 10; i++) { if (i === 5) { break; // stop the loop when i is 5 } console.log(i); } console.log('Loop finished');

Result:

text
0 1 2 3 4 Loop finished

The loop stops as soon as i becomes 5, and the remaining iterations never run.

break in for, while and do...while loops

break ends the loop immediately and passes control to the next line of code after it. It is the main tool for searching: once the element you need is found, there is no point in scanning the rest.

javascript
const users = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Maria' }, ]; let found = null; for (const user of users) { if (user.id === 2) { found = user; break; // no reason to keep scanning } } console.log(found); // { id: 2, name: 'Bob' }

The same works in while and do...while. One important detail: break leaves only one loop, the nearest one, so in nested loops it stops the inner loop only.

break in a switch

Here break is used to leave the switch block after the required case has run. Without it, execution falls through into the next case.

javascript
const day = 2; switch (day) { case 1: console.log('Monday'); break; case 2: console.log('Tuesday'); break; case 3: console.log('Wednesday'); break; default: console.log('Another day'); }

Result:

text
Tuesday

If case 2 had no stop in it, every following case would run, up to the nearest break or the end of the block.

Labelled break in nested loops

When you need to leave several levels of nesting at once, label the loop and break out of that label.

javascript
outer: for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (i * j === 2) break outer; // leave both loops at once console.log(i, j); } }

Result:

text
0 0 0 1 0 2 1 0 1 1

An alternative to a label is extracting the nested loops into a separate function and leaving it with return, which is often easier to read.

Comparison with continue

KeywordWhat it doesWhere it works
breakStops the loop completely, or exits a switchLoops, switch
continueSkips the rest of the current iterationLoops only

Common mistakes

  • Using break inside forEach, map or filter. These are methods, not loops, so it is a SyntaxError; for an early exit use for...of, some or find.
  • Forgetting break in a switch branch and getting fall-through, that is, several branches running instead of one.
  • Assuming break in nested loops leaves all of them. It leaves only the nearest one; anything further needs a label.
  • Confusing break with continue: the first ends the loop, the second only skips a step.
  • Placing break inside an if block that is not in a loop. Outside a loop or a switch it is a syntax error.
  • Expecting break to cancel a finally block: code in try ... finally still runs before control leaves.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.