By the end, you can use break to stop a JavaScript loop early and explain how it differs from continue and return.
What it is
break is a control-flow statement that immediately ends the innermost loop or switch block in which it appears. Think of it as an emergency exit: when the condition is met, JavaScript stops repeating the loop body and continues with the first statement after the loop.
Related terms include loop (for, while, do...while, for...of, for...in), switch, label, continue, and return.
Why it matters
- It avoids unnecessary work when you already found what you need, such as the first negative number or first match.
- It prevents infinite loops when a condition inside the loop body should stop repetition.
- It makes search logic clearer than adding complex loop conditions.
- It controls
switchbehavior by stopping fallthrough to later cases.
Syntax or steps
The smallest useful pattern is a loop with a condition that calls break:
for (const item of items) {
if (stopCondition) {
break;
}
}
Steps:
- Choose a loop that can be exited early.
- Test the stopping condition inside the loop body.
- Call
break;when the condition is true. - Let execution continue after the loop.
For nested loops, break; exits only the innermost loop. To exit an outer loop, label it and use break outerLabel;.
Example
const nums = [3, 7, -2, 5];
for (const n of nums) {
if (n < 0) {
break;
}
console.log(n);
}
console.log("done");
Output:
3
7
done
Part by part:
const nums = [3, 7, -2, 5];creates an array to search.for (const n of nums)visits each number in order.if (n < 0)checks whether the current number is negative.break;stops the loop when-2is found, so5is never checked.console.log("done");runs after the loop becausebreakexits the loop, not the program.
Common mistakes
- Using
breakoutside a loop orswitch. This causes aSyntaxError. Put it inside a valid loop or switch block. - Expecting
breakto exit a function. It only exits the loop. Usereturnif you need to leave the function. - Trying to
breakinsideforEach. The callback is a function, not a loop body, sobreakis invalid there. Usefor...of,some(), orevery()instead. - Forgetting
breakin aswitchcase. Execution can fall through to the next case. Addbreak;unless fallthrough is intentional.
When to use it
Use break when you want to stop repeating entirely. Use continue when you want to skip only the current iteration. Use return when you want to leave the whole function.
| Statement | Effect | Best for |
|---|---|---|
break | Ends the innermost loop or switch | Found target, invalid data, stop condition |
continue | Skips to next iteration | Ignore one item but keep searching |
return | Exits the function | Finish processing and give a result |
Practice
Guided exercise: Write a loop over [10, 20, 30, 40] that prints each number until it reaches 30, then stops.
const nums = [10, 20, 30, 40];
for (const n of nums) {
if (n === 30) {
break;
}
console.log(n);
}
Expected output: 10 and 20.
Challenge: Use a labeled break to stop both loops when i * j is greater than 6.
outer:
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (i * j > 6) {
break outer;
}
console.log(i, j);
}
}
Hint: the label outer lets break outer; exit the outer loop instead of only the inner loop.
Quick check
Question: What happens to the code after a loop when break runs inside the loop?
Answer: The loop stops immediately, and execution continues with the next statement after the loop.
Summary
break is JavaScript’s way to stop a loop or switch early. It is useful for searches and guard conditions, but it only exits the innermost loop unless you use a label.