Break & Continue
in JavaScript
Master the two keywords that give you precise control over loop execution: break to exit early, continue to skip ahead.
What Are Loop Control Statements?
By default, a loop runs from start to finish, executing every iteration. But sometimes you need to alter that flow: stop early when you find what you need, or skip certain iterations that don't matter.
JavaScript gives you two keywords for this: break and continue. They work inside for, while, and do...while loops.
The break Statement
break immediately terminates the innermost loop it is inside. Execution jumps to the first statement after the loop.
The continue Statement
continue skips the rest of the current iteration and jumps to the loop's next cycle (the update expression in a for loop, or the condition check in a while loop).
How Flow Changes Inside a Loop
The Core Rule
break in a while Loop
break is especially useful in while loops where you don't always know when to stop ahead of time.
Key Distinction: break vs continue
| Feature | break | continue |
|---|---|---|
| Effect | Exits the loop | Skips to next iteration |
| Remaining code | Never runs | Skipped for this pass only |
| Loop condition | Not re-checked | Re-checked before next pass |
| Common use | Search found, early exit | Filter/skip unwanted items |
| Works in | for, while, do...while, switch | for, while, do...while |
How to Apply Them
Example 1: Finding a Value
Search an array for the first negative number. Once found, stop searching.
Example 2: Skipping Odd Numbers
Print only even numbers from 1 to 10 using continue.
Example 3: Nested Loops
break only exits the innermost loop. The outer loop continues.
Labeled break & continue
To break out of an outer loop from inside an inner one, use a label.
When to Use Each One
Common Mistakes
Summary Table
| Scenario | Keyword | Why |
|---|---|---|
| Find first match in array | break | No need to keep searching |
| Skip invalid input | continue | Process the rest normally |
| Exit infinite while loop | break | Only way to stop |
| Skip even/odd numbers | continue | Filter within the loop |
| Exit nested loops | break + label | Escape multiple levels |
Question 1 of 5
What is the output of this code?
Question 2 of 5
What is the output of this code?
Question 3 of 5
Which keyword would you use to stop searching an array once you've found the value you need?
Question 4 of 5
What is the danger of using continue in a while loop?
Question 5 of 5
In nested loops, what does a plain break (without a label) do?