break and continue

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.

break-and-continue.js
for (let i = 1; i <= 10; i++) {
if (i === 4) {
break; // stop the loop when i reaches 4
}
console.log(i);
}
// Output: 1 2 3

Let’s walk through what happens here:

  • The for loop is set up to count i from 1 all the way to 10.
  • On each turn, the if checks whether i has reached 4.
  • For 1, 2, and 3 the check is false, so console.log(i) runs and prints the number.
  • When i becomes 4, the check is true, break runs, and the loop ends immediately, so 4 through 10 never print. That is why we only see 1, 2, and 3.

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.

break-and-continue.js
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue; // skip when i is 3
}
console.log(i);
}
// Output: 1 2 4 5

Let’s walk through what happens here:

  • The loop counts i from 1 to 5.
  • For 1 and 2 the if is false, so console.log(i) runs as normal.
  • When i is 3, the if is true and continue runs, jumping past the console.log below it, so 3 is never printed.
  • The loop does not stop, so it carries on and prints 4 and 5.

🔍 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:

break-and-continue.js
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 2

Let’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") and 1 ("Sam") the match is false, so nothing happens.
  • At position 2 we find "Jordan", print the message, and break ends 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:

break-and-continue.js
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); // 60

Let’s walk through what happens here:

  • We start with a total of 0 and loop over every score in the array.
  • The if checks whether the current score is negative.
  • For -5 and -1 the check is true, so continue skips the line that adds to total.
  • For 10, 20, and 30 the check is false, so each one is added on. After the loop, total holds 60.

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:

break-and-continue.js
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, break is the very first line, so the loop ends before it reaches console.log(i). That line is unreachable and prints nothing.
  • In the second loop, console.log(i) runs first and prints 0, and only then does break end 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!

  1. Write a for loop that counts from 1 to 10 and uses break to stop at 7.
  2. Write a for loop from 1 to 10 that uses continue to skip the number 5.
  3. Create an array of names and use break to stop at the first match.
  4. Create an array of numbers with some negatives and use continue to add only the positive ones.

🧩 What You’ve Learned

  • break exits a loop entirely and jumps to the code after it
  • continue skips the current iteration and moves on to the next one
  • break is great for stopping once you find a match
  • continue is great for skipping values you want to ignore
  • ✅ Both keywords only work inside a loop, and code after break never 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.

Share & Connect