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
breakimmediately ends the nearest enclosing loop orswitchblock.- In loops it is used to stop iterating once the result has been found.
- In a
switchit prevents execution from falling through into the followingcaseclauses. breakmay only appear inside a loop or aswitch, otherwise you get aSyntaxError.- To skip a single iteration instead of ending the loop, use
continue. - With a label,
break labelexits the outer loop rather than the inner one.
Quick example
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:
0
1
2
3
4
Loop finishedThe 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.
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.
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:
TuesdayIf 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.
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:
0 0
0 1
0 2
1 0
1 1An 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
| Keyword | What it does | Where it works |
|---|---|---|
break | Stops the loop completely, or exits a switch | Loops, switch |
continue | Skips the rest of the current iteration | Loops only |
Common mistakes
- Using
breakinsideforEach,maporfilter. These are methods, not loops, so it is aSyntaxError; for an early exit usefor...of,someorfind. - Forgetting
breakin aswitchbranch and getting fall-through, that is, several branches running instead of one. - Assuming
breakin nested loops leaves all of them. It leaves only the nearest one; anything further needs a label. - Confusing
breakwithcontinue: the first ends the loop, the second only skips a step. - Placing
breakinside anifblock that is not in a loop. Outside a loop or aswitchit is a syntax error. - Expecting
breakto cancel afinallyblock: code intry ... finallystill runs before control leaves.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.