How to Use Break and Continue in JavaScript
Use break and continue when a loop should stop early or skip only the current cycle. These two keywords give you much tighter control over loop flow without rewriting the full loop condition.
What you’ll build or solve
You’ll learn how to use break and continue in JavaScript loops. You’ll also know when to stop the loop completely versus skipping only one iteration.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when a loop needs exceptions, early exits, or skip rules.
Common real-world scenarios include:
- Stop after finding a match
- Skip invalid records
- Exit retry loops
- Ignore empty values
- Search optimization
This is a bad idea when the same result can be expressed more clearly with array methods like find() or filter().
Prerequisites
You only need:
- A JavaScript file or browser console
- Basic loops and conditionals knowledge
Step-by-step instructions
Step 1: Use break to stop and continue to skip
Use break when the loop should end immediately.
JavaScript
for (let i = 0; i < 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
This stops the loop as soon as i reaches 5.
Use continue when only the current cycle should be skipped.
JavaScript
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue;
}
console.log(i);
}
This skips printing 2 but keeps the loop running.
What to look for:
breakexits the whole loopcontinueskips one cycle- Great for search and validation loops
- Place them inside conditions
- Use sparingly to keep loops readable
Examples you can copy
Stop on match
JavaScript
for (const user of users) {
if (user.id === 42) {
break;
}
}
Skip empty values
JavaScript
for (const item of items) {
if (item === "") {
continue;
}
}
Retry exit
JavaScript
while (attempts < 5) {
if (success) {
break;
}
}
Common mistakes and how to fix them
Mistake 1: Using break when only one item should skip
What the reader might do:
JavaScript
if (item === "") {
break;
}
Why it breaks: the loop stops completely instead of skipping the invalid item.
Corrected approach:
JavaScript
if (item === "") {
continue;
}
Mistake 2: Using continue without clear conditions
What the reader might do:
JavaScript
continue;
console.log(item);
Why it breaks: the rest of the loop body becomes unreachable.
Corrected approach:
Place continue inside a specific condition block.
Mistake 3: Overusing loop control in simple searches
What the reader might do:
JavaScript
for (const user of users) {
if (user.active) {
break;
}
}
Why it breaks: array helpers may read better.
Corrected approach:
JavaScript
const activeUser = users.find(user => user.active);
Troubleshooting
If the loop stops too early, check accidental break conditions.
If items disappear unexpectedly, inspect continue logic.
If the loop becomes hard to read, switch to array helpers.
If retries never stop, confirm the break condition can actually become true.
Quick recap
- Use
breakto stop the loop - Use
continueto skip one cycle - Place both inside clear conditions
- Great for searches and validation
- Prefer array helpers when cleaner
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot