break and continue
Table of Contents + −
In the previous lesson, we learned how the do...while loop runs its body at least once before checking the condition. Now let’s learn how to take control of a loop while it is running, using break and continue.
🛑 What are break and continue?
Sometimes we do not want a loop to run all the way through. We might want to stop early once we find what we are looking for, or skip over a value we do not care about. JavaScript gives us two keywords for this: break and continue.
break stops the loop completely and jumps to the code after it. continue skips the rest of the current turn and moves on to the next one.
| Keyword | What it does | The loop after it |
break | Exits the loop entirely | Stops running |
continue | Skips the current iteration | Keeps running with the next value |
🚪 Using break to Exit a Loop
The break keyword ends the loop right away. As soon as JavaScript reaches it, the loop stops and the program continues below.
for (let i = 1; i <= 10; i++) { if (i === 4) { break; // stop the loop when i reaches 4 } console.log(i);}// Output: 1 2 3Let’s walk through what happens here:
- The
forloop is set up to countifrom1all the way to10. - On each turn, the
ifchecks whetherihas reached4. - For
1,2, and3the check is false, soconsole.log(i)runs and prints the number. - When
ibecomes4, the check is true,breakruns, and the loop ends immediately, so4through10never print. That is why we only see1,2, and3.
break stops everything
break does not just skip one number. It ends the whole loop, no matter how
many iterations were left.
⏭️ Using continue to Skip an Iteration
The continue keyword skips the rest of the current iteration and jumps straight to the next one. The loop keeps going, it just ignores the code below continue for that turn.
for (let i = 1; i <= 5; i++) { if (i === 3) { continue; // skip when i is 3 } console.log(i);}// Output: 1 2 4 5Let’s walk through what happens here:
- The loop counts
ifrom1to5. - For
1and2theifis false, soconsole.log(i)runs as normal. - When
iis3, theifis true andcontinueruns, jumping past theconsole.logbelow it, so3is never printed. - The loop does not stop, so it carries on and prints
4and5.
🔍 A Practical Example
In real programs we often loop through a list of items. break lets us stop as soon as we find a match, and continue lets us skip items we do not want to process.
Here we search a list of names and stop the moment we find the one we want:
const names = ["Alex", "Sam", "Jordan", "Taylor"];
for (let i = 0; i < names.length; i++) { if (names[i] === "Jordan") { console.log("Found Jordan at position " + i); break; // no need to keep searching }}// Output: Found Jordan at position 2Let’s walk through what happens here:
- We start with an array of four names and loop over it by index
i. - On each turn we compare
names[i]against"Jordan". - At positions
0("Alex") and1("Sam") the match is false, so nothing happens. - At position
2we find"Jordan", print the message, andbreakends the loop so we never check"Taylor". That saves work, since there is no reason to keep searching after a match.
Now let’s use continue to skip invalid values and only add up the valid ones:
const scores = [10, -5, 20, -1, 30];let total = 0;
for (let i = 0; i < scores.length; i++) { if (scores[i] < 0) { continue; // skip negative scores } total += scores[i];}
console.log(total); // 60Let’s walk through what happens here:
- We start with a
totalof0and loop over every score in the array. - The
ifchecks whether the current score is negative. - For
-5and-1the check is true, socontinueskips the line that adds tototal. - For
10,20, and30the check is false, so each one is added on. After the loop,totalholds60.
break for finding, continue for filtering
Reach for break when you only need the first match and can stop searching.
Reach for continue when you want to skip some values but still process the
rest.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
Confusing break with continue | break ends the loop, continue only skips one turn | Use break to stop, continue to skip |
| Using them outside a loop | break or continue on their own causes a syntax error | Only use them inside a loop body |
Code after break | Lines below break never run | Put any needed code before break |
Here is the unreachable-code mistake in action:
for (let i = 0; i < 5; i++) { break; console.log(i); // ❌ Wrong: this line never runs}
for (let i = 0; i < 5; i++) { console.log(i); // ✅ Correct: runs before we break break;}Let’s compare the two loops:
- In the first loop,
breakis the very first line, so the loop ends before it reachesconsole.log(i). That line is unreachable and prints nothing. - In the second loop,
console.log(i)runs first and prints0, and only then doesbreakend the loop. - The lesson: any code you need has to come before
break, because nothing below it in that turn will run.
🔧 Try It Yourself!
- Write a
forloop that counts from1to10and usesbreakto stop at7. - Write a
forloop from1to10that usescontinueto skip the number5. - Create an array of names and use
breakto stop at the first match. - Create an array of numbers with some negatives and use
continueto add only the positive ones.
🧩 What You’ve Learned
- ✅
breakexits a loop entirely and jumps to the code after it - ✅
continueskips the current iteration and moves on to the next one - ✅
breakis great for stopping once you find a match - ✅
continueis great for skipping values you want to ignore - ✅ Both keywords only work inside a loop, and code after
breaknever runs
🚀 What’s Next?
Now that we can control how loops run, we will learn how to organize code into reusable blocks. Let’s continue to Functions.