Leave a Loop Early With break and continue

Back in the sunflower example, the loop ended only when its condition turned false, and the lesson promised that you’d later meet a way to leave a loop early. This is that lesson.

Sometimes a loop’s job is done before that. Once your patient guesser from the first lesson has found the secret code, why keep counting? In this lesson, you’ll use break to stop a loop at the first match and continue to skip a pass you don’t need. You’ll also build the loop shape your guessing game will use, and you’ll see why continue needs extra care in a while loop.

Stop at the First Match With break

Here’s the patient guesser again, this time as a for loop that tries the numbers from 1 to 10. The secret code is 4, so the loop’s work is done on the fourth pass. Save this as first_match.js:

Language: JavaScript Filename: first_match.js
const secret = 4;

for (let guess = 1; guess <= 10; guess++) {
  if (guess === secret) {
    console.log("Found it:", guess);
    break;
  }
  console.log(guess, "is not the code");
}

console.log("Done guessing");

The new line is break; inside the if. Before you run the script, predict how many lines it prints. Then give it a spin:

Language: Shell
$ node first_match.js
1 is not the code
2 is not the code
3 is not the code
Found it: 4
Done guessing

The break statement ends the whole loop right away. JavaScript skips the rest of the body, doesn’t run the update, and doesn’t check the condition again. It carries on with the first line after the loop’s closing brace, which prints Done guessing.

Notice how the work is split. The if decides when to leave, and break does the leaving. To see what the break saves you from, make a copy called no_break.js and delete the break; line:

Language: JavaScript Filename: no_break.js
const secret = 4;

for (let guess = 1; guess <= 10; guess++) {
  if (guess === secret) {
    console.log("Found it:", guess);
  }
  console.log(guess, "is not the code");
}

console.log("Done guessing");

Run it and compare the output with the first version:

Language: Shell
$ node no_break.js
1 is not the code
2 is not the code
3 is not the code
Found it: 4
4 is not the code
5 is not the code
6 is not the code
7 is not the code
8 is not the code
9 is not the code
10 is not the code
Done guessing

Without break, the loop runs all ten passes. It even claims that 4 is not the code right after finding it, because the rest of the body still runs on that pass. With break, the loop stops at the first match, and the rest of the loop body doesn’t run. You can delete no_break.js now.

Skip a Pass With continue

Sometimes you don’t want to stop the whole loop, only to skip one pass. Say a player writes the secret code with a dash in the middle, like "27-49", because it’s easier to read. Your game only cares about the digits, so the loop should pass over the dash and keep going. Save this as clean_code.js:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.