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.
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.
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.
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.
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.
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.
<!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
- In a
forover[3, 9, 1],breakwhen the value is9and store it. - Log only odd numbers from
1to6usingcontinueon evens. - In a
while, stop withbreakwhen a counter hits3. - With
for...ofon["ok", "no", "ok"],continuepast"no". - Join
["1", "-", "2"]into12by skipping"-". - In the HTML demo, change only the array, refresh, and read
kept=.
Common mistakes
- Thinking
continueexits the whole loop (that isbreak) - Putting more work after
breakin the same round — it never runs - In a
while, usingcontinuewithout updating the counter first — endless loop risk - Using
breakwhen you only meant to skip one item - Expecting
breakto leave the whole script — it only leaves the nearest loop
Summary
break— leave the loop now; later rounds do not runcontinue— skip the rest of this round; start the next- Works in
for,while, andfor...of - Search patterns often use
break; filter patterns often usecontinue - Code after the loop still runs after
break - Next: functions — name a block of work and call it
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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