JavaScript break and continue: definition, example, and difference

This article is created to define and differentiate between the two important JavaScript keywords, namely:

The break statement in JavaScript

The break statement, or keyword, in JavaScript is used to jump out of the current loop.

The break statement syntax in JavaScript

The syntax of the break statement in JavaScript is:

break;

The break statement example in JavaScript

Consider the following code as an example demonstrating the "break" statement or keyword in JavaScript:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      for(let i=0; i<=10; i++)
      {
         if(i==4)
            break;
         console.log(i);
      }
   </script>
   
</body>
</html>

The output produced by the above JavaScript example on the "break" statement is:

js break statement

As you can see from the above example, if the value of i becomes 4, then the break statement gets executed to break out of the current loop or to stop the execution of the current loop.

The continue statement in JavaScript

The JavaScript continue statement or keyword is used to skip the execution of statement(s) for the current iteration of the current loop.

The continue statement syntax in JavaScript

The syntax of the continue statement in JavaScript is:

continue;

The continue statement example in JavaScript

Consider the following code as an example demonstrating the "continue" statement in JavaScript:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      for(let i=0; i<=5; i++)
      {
         if(i==3)
            continue;
         console.log(i);
      }
   </script>
   
</body>
</html>

The snapshot given below shows the sample output produced by the above JavaScript example on the continue statement:

continue statement in javascript

As you can see from the above example, when the value of i becomes 3, the continue statement gets executed to skip the remaining execution of the statement(s) for the current iteration.

Note: Only the statement(s) after the continue statement will be skipped, for the current iteration of the current loop, of course. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      for(let i=0; i<=5; i++)
      {
         console.log(i);
         if(i==3)
            continue;
         console.log(i);
      }
   </script>
   
</body>
</html>

The sample output of this example is shown in the following snapshot:

javascript continue statement

In the above example, when the value of i becomes 3, the above two statements are executed, whereas the below two statements (the two statements after the continue statement) are skipped.

Difference between break and continue in JavaScript

The table given below differentiates between "break" and "continue" statements in JavaScript.

break continue
jumps out of the current loop jumps to the next iteration of the current loop.
skips the remaining executions of the current loop skips execution of the remaining statement(s) inside the current loop for the current iteration
stops the execution of the current loop stops the execution of the remaining statement(s) inside the current loop for the current iteration
useful if condition always evaluates to true useful, if one wants to skip

break vs. continue example in JavaScript

The following example demonstrates the difference between break and continue statements in JavaScript:

<!DOCTYPE html>
<html>
<body>
   
   <script>
      const nums = [1, 2, 3, 4, 5];
      
      console.log("Using the 'break' Keyword");
      for(let i in nums)
      {
         if(nums[i]==3)
            break;
         console.log(nums[i]);
      }

      console.log("Using the 'continue' Keyword");
      for(let i in nums)
      {
         if(nums[i]==3)
            continue;
         console.log(nums[i]);
      }
   </script>
   
</body>
</html>

The snapshot given below shows the sample output produced by the above JavaScript example on the break vs. continue statement:

break vs continue javascript

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!