break
Stops the nearest loop or switch.
break stops a loop or switch. continue skips the rest of the current loop iteration and moves to the next one.
for (const record of records) {
if (!record.active) {
continue;
}
if (record.id === targetId) {
break;
}
}
Control Flow
break exits the nearest loop or switch immediately. It is useful when the work is complete, a match is found or a limit is reached.
continue skips the remaining statements in the current loop body and starts the next iteration. It is useful for ignoring invalid, inactive or irrelevant items early.
Both keywords can make loops clearer when used sparingly. If every few lines contain break or continue, the loop probably needs restructuring.
Stops the nearest loop or switch.
Skips to the next loop iteration.
Use continue near the top of a loop for invalid items.
Use break when continuing would waste work.
Examples
let match = null;
for (const record of records) {
if (!record.active) {
continue;
}
if (record.id === targetId) {
match = record;
break;
}
}
for (const record of records) {
if (record.active) {
if (record.id === targetId) {
match = record;
break;
}
}
}
// The same behavior is harder to scan because the valid path is nested.
Rules that matter
Control flow decides which path code takes. Keep that path visible: name conditions, protect loop boundaries, avoid accidental fallthrough and use early exits only when they make the main path clearer.
It keeps the valid path less indented.
Do not keep searching after the answer is found.
Too many exits make flow hard to trace.
break and continue affect the nearest loop, not every outer loop.
Labeled break exists, but most code is clearer with functions.
find, some and every can communicate intent better than manual loops.
Production thinking
Large loops become readable when irrelevant items are skipped and completed searches stop immediately.
Skipping invalid data can prevent broken labels, empty controls and incomplete messages from entering the interface.
Production loops should be efficient without hiding intent. break and continue should make the loop easier, not cleverer.
When loops build links or content, continue can skip bad data and break can stop after a useful limit.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Read each question out loud and answer it without looking at the examples. Control flow only clicks when you can trace the path in your own words.
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Refactor a nested if/else example into guard clauses or a switch/lookup object.
Is the new control flow easier to read under pressure?