Please enable JavaScript to use CodeHS

Break and Continue in JavaScript

Learn how to use the break and continue statements in your JavaScript programs.

By Rachel Devaney

JavaScript has some nifty tools we can use to tweak the behaviors of for loops and while loops. In this tutorial, we’ll learn about break and continue.

Break

The break statement enables us to immediately exit a loop without executing any of the remaining code in the loop. Typically, we use break statements with a conditional because we want to exit the loop when a specific condition is met. At a high level, a break statement may look like this:

while(condition){
     // code that executes in the loop
     if (condition) {
          break; // immediately exits the loop
     }
    // code that executes in the loop, but is skipped if the break condition is true
}
Plain text

Break + While Loops

break statements work well with while loops because they avoid repeated code. Take a look at the following code that asks the user to guess a secret number between 1 and 10. This program works, however lines 2 and 6 repeat the same code.

var secretNum = 6;
var guess = readInt("Guess a number, 1-10: ");

while(guess != secretNum){
     println("Wrong. Try again.");
     guess = readInt("Guess a number, 1-10: ");
}
println("You win!");
Plain text

The program below completes the same task but uses a break statement. Now, instead of repeating the readInt() command before and inside of the while loop, we just use it once inside the loop.

var secretNum = 6;

while(true){
    var guess = readInt("Guess a number, 1-10: ");
    if(guess == secretNum){
        println("You win!");
        break;
    }
    println("Wrong. Try again.");
}
Plain text

To do this, we use the condition while(true) for the loop and move the condition to exit the loop, if(guess == secretNum), to the break statement. While using while(true) could result in an infinite loop, we avoid this by using a condition with the break statement.

Run the example below to see the break statement in action. What happens when you move the println("You win!") to after the break statement? Why is this the case?



Break + For Loops
You can also use the break statement in for loops. Take a look at the example for loop below.

  • Try guessing what will happen before you run it!
  • Try changing the condition for the break statement.
  • What happens if you move the println(i) command to before the if statement?

Continue

While the break statement immediately exits the loop, the continue statement immediately returns to the condition of the loop. This enables us to skip specific iterations of the loop without exiting the entire loop. Take a look at the example program below. It’s exactly the same as the for loop above, except that there is a continue statement where the break statement was.

  • Try guessing what will happen before you run it.
  • Try changing the condition of the continue statement.
  • What happens if you move the println(i) command to before the if statement?

Your Turn!

Try your hand at break and continue statements in the code editor below. Here are some practice ideas:

  • Write a program that prints out the numbers 1-20 but skips the unlucky number 13.
  • Write a program that finds the product of a series of numbers that the user enters and prints the result to the screen. You should continue to ask the user for a number until the user enters -1. Multiply each number the user enters and print it to the screen. Hint: You will need to store the product in a variable, and you can use the readInt(prompt) command to get a number from the user.
  • Extend your product program! Continue asking the user for a number until the user enters -1 or the product is greater than 200.