01 / 21

Break & Continue
in JavaScript

Master the two keywords that give you precise control over loop execution: break to exit early, continue to skip ahead.

JavaScript

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.

for (let i = 0; i < 10; i++) { if (i === 5) { break; // exit the loop entirely } console.log(i); } // Output: 0, 1, 2, 3, 4

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).

for (let i = 0; i < 6; i++) { if (i === 3) { continue; // skip this iteration } console.log(i); } // Output: 0, 1, 2, 4, 5

How Flow Changes Inside a Loop

Normal Loop Start Code Block
Update \(i\!+\!+\)
repeat break Loop Start break; After Loop EXIT continue Loop Start continue; SKIP
Update \(i\!+\!+\)
Skipped Code

The Core Rule

break
Terminates the entire loop. No more iterations run. Execution moves to the code after the loop.
continue
Terminates the current iteration only. The loop continues with the next iteration as normal.

break in a while Loop

break is especially useful in while loops where you don't always know when to stop ahead of time.

let input; while (true) { // infinite loop input = prompt("Enter 'quit' to stop:"); if (input === "quit") { break; // escape the infinite loop } console.log("You typed:", input); }

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

1
Identify the goal. Do you need to stop entirely, or just skip certain items?
2
Choose the keyword. Use break to exit early. Use continue to skip and keep going.
3
Write the condition. Place an if statement that triggers the keyword at the right moment.
4
Place it correctly. Put the keyword before the code you want to skip or prevent from running.
5
Test edge cases. What if the condition triggers on the first iteration? The last? Never?

Example 1: Finding a Value

Search an array for the first negative number. Once found, stop searching.

const nums = [4, 7, 2, -3, 9, -1]; let firstNeg = null; for (let i = 0; i < nums.length; i++) { if (nums[i] < 0) { firstNeg = nums[i]; break; // found it, stop! } } console.log(firstNeg); // -3

Example 2: Skipping Odd Numbers

Print only even numbers from 1 to 10 using continue.

for (let i = 1; i <= 10; i++) { if (i % 2 !== 0) { continue; // skip odd numbers } console.log(i); } // Output: 2, 4, 6, 8, 10

Example 3: Nested Loops

break only exits the innermost loop. The outer loop continues.

for (let row = 0; row < 3; row++) { for (let col = 0; col < 3; col++) { if (col === 1) { break; // breaks inner loop only } console.log(row, col); } } // Output: (0,0), (1,0), (2,0)

Labeled break & continue

To break out of an outer loop from inside an inner one, use a label.

outer: for (let i = 0; i < 5; i++) { for (let j = 0; j < 5; j++) { if (i + j === 4) { break outer; // exits BOTH loops } } } // Stops at i=0, j=4 (first pair summing to 4)

When to Use Each One

Use break when...
You found what you need and continuing would waste time. Searching, validating, or handling an exit condition.
Use continue when...
Some items don't need processing. Filtering data, skipping invalid entries, or handling edge cases within a loop.

Common Mistakes

✕ Infinite loop with continue
In a while loop, placing continue before the counter update means the counter never changes: infinite loop.
✕ break in wrong scope
Using break inside a nested loop thinking it exits the outer loop. It only exits the innermost loop.
✕ Unreachable code after break
Placing code after break or continue inside the same block. That code never runs and may cause confusion.

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?

for (let i = 0; i < 5; i++) { if (i === 3) break; console.log(i); }

Question 2 of 5

What is the output of this code?

for (let i = 0; i < 5; i++) { if (i === 2) continue; console.log(i); }

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?