Chapter V · Control Flow

The break Statement

break exits a loop or switch immediately, without waiting for its normal condition to fail — useful the moment you already have what you were looking for.

01 What Is the break Statement?

break is a jump statement: when the engine reaches it, it stops running the current loop or switch entirely — including skipping any remaining iterations — and resumes execution at the first statement after that loop or switch. It only affects the closest enclosing loop or switch, unless a label is used to target an outer one.

02 Simple Example

Stopping a loop as soon as a value is found.

break.js
for (let i = 0; i < 10; i++) {\n  if (i === 3) break;\n  console.log(i);\n}\n// logs 0, 1, 2

The loop was set up to run ten times, but break exits on the fourth iteration (i === 3) — 4 through 9 never happen.

03 How It Works

break doesn't just skip the rest of the current iteration — that's what continue does. It ends the entire loop structure, discarding any remaining planned iterations, and control jumps straight to the code that follows the loop's closing brace.

04 Practical Example — Searching an Array

A common real-world pattern: stop scanning the moment a match is found.

search.js
const numbers = [10, 20, 50, 30];\nlet targetIndex = -1;\n\nfor (let i = 0; i < numbers.length; i++) {\n  if (numbers[i] === 50) {\n    targetIndex = i;\n    break;\n  }\n}\nconsole.log(targetIndex); // 2

Without break, the loop would keep scanning numbers[3] and beyond even after finding the target — harmless here, but wasteful on a large array. Array methods like findIndex do the equivalent internally.

05 Common Mistakes & Edge Cases

break inside nested loops only exits the innermost one by default. To exit an outer loop from inside a nested one, JavaScript supports labeled statements: outer: for (...) { for (...) { break outer; } }.

break is also what stops switch fall-through — its most common use outside of loops.

06 Mental Model

"An emergency exit — you leave the whole building immediately, not just the current room."

07 Interview Questions

Q: How do you break out of a nested loop from the inner one?

Label the outer loop (e.g. outer: for...) and use break outer; inside the inner loop — a plain break there would only exit the inner loop.

Key Takeaway

break immediately terminates the closest enclosing loop or switch — reach for it the moment further iterations would be wasted work, and use a label if you need to exit an outer loop from inside a nested one.

Related Concepts