Loading...

JavaScript Break and Continue

Loops can run every round to the end — until you steer them. Sometimes you already have the answer and want to stop. Sometimes one item is junk and you want to skip it, then keep going. break leaves the loop immediately. continue jumps to the next round. Same loop family you already know; two small words change the flight path.

What does break do?

break ends the nearest loop right now. No later rounds run. Whatever comes after the loop in your script still runs. Think “I’m done — leave.” Common when you search for the first match and do not need to scan the rest.

break vs continue

📊 Steer a loop mid-flight

Keyword Effect Everyday fit
break leave the loop now found the first match; stop waiting
continue skip the rest of this round, start the next ignore one bad item; keep scanning
neither every round runs the full body you need every item processed the same way
after the loop code below the loop still runs break only exits the loop, not the whole script

Pick break when later rounds are useless. Pick continue when one round should be quiet but the walk must continue. Do not swap them — the bug looks “almost right” and is hard to spot.

break stops a for loop

javascript
const nums = [2, 4, 7, 8];
let found = null;
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 7) {
found = nums[i];
break;
}
}
console.log(found);

Stop when you find a match

The loop checks 2, then 4, then 7. On 7 it stores the value and breaks. 8 never runs. found is 7. Without break, the loop would keep walking after the answer was already known.

continue skips one round

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

Skip a round and keep going

When i is even, continue jumps to the next i. The console.log line is skipped for those rounds. You see 1, 3, 5. The loop did not stop — it only passed over even numbers.

break inside while

javascript
let n = 1;
while (n < 20) {
if (n === 5) {
break;
}
console.log(n);
n = n + 1;
}

Leave a while early

while would keep going while n < 20. At n === 5, break exits. You printed 1–4. This pattern fits “keep asking until a stop rule,” then leave without finishing every possible round.

continue with for...of

javascript
const colors = ["red", "skip", "blue"];
for (const c of colors) {
if (c === "skip") {
continue;
}
console.log(c);
}

Skip values in for...of

for...of still walks values. continue ignores "skip" and logs red, then blue. Steering works the same idea across for, while, and for...of — nearest loop only.

Build a line while skipping

javascript
const parts = ["A", "x", "B", "x", "C"];
let msg = "";
for (const p of parts) {
if (p === "x") {
continue;
}
msg = msg + p;
}
console.log(msg);

Filter while you join

Each "x" is skipped; letters append. Result is ABC. Same join habit as earlier loop lessons — continue is the filter gate inside the walk.

Show break and continue on the page

Walk a short list, skip one label with continue, stop early with break after a marker, write the result into the page.

Page walk with skip and stop

html
<!DOCTYPE html>
<html>
<head>
<title>break / continue — page</title>
</head>
<body>
<p id="out">…</p>
<script>
const items = ["a", "skip", "b", "stop", "c"];
let line = "";
for (const item of items) {
if (item === "skip") {
continue;
}
if (item === "stop") {
break;
}
line = line + item + " ";
}
let box = document.getElementById("out");
box.textContent = "kept=" + line.trim();
</script>
</body>
</html>

The page shows kept=a b. "skip" was ignored; "stop" ended the loop before "c". Change the array, refresh, and read the new line. Next lesson starts functions — reusable blocks of code you can call by name.

Try it yourself

  1. In a for over [3, 9, 1], break when the value is 9 and store it.
  2. Log only odd numbers from 1 to 6 using continue on evens.
  3. In a while, stop with break when a counter hits 3.
  4. With for...of on ["ok", "no", "ok"], continue past "no".
  5. Join ["1", "-", "2"] into 12 by skipping "-".
  6. In the HTML demo, change only the array, refresh, and read kept=.

Common mistakes

  • Thinking continue exits the whole loop (that is break)
  • Putting more work after break in the same round — it never runs
  • In a while, using continue without updating the counter first — endless loop risk
  • Using break when you only meant to skip one item
  • Expecting break to leave the whole script — it only leaves the nearest loop

Summary

  • break — leave the loop now; later rounds do not run
  • continue — skip the rest of this round; start the next
  • Works in for, while, and for...of
  • Search patterns often use break; filter patterns often use continue
  • Code after the loop still runs after break
  • Next: functions — name a block of work and call it

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
8
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top