Difference Between break and continue in JavaScript

This article is created to differentiate between the two important keywords of JavaScript, that are:

break Vs. continue in JavaScript

The table given below differentiates break and continue statements/keywords in JavaScript:

break continue
jumps out from the current loop jumps for the next iteration of the current loop
skips remaining executions of the current loop skips execution of remaining statement(s) inside the current loop for current iteration
stops the execution of the current loop stops the execution of the remaining statement(s) inside the current loop for current iteration
useful, if condition always evaluates to be True useful, if wants to skip the execution of statement(s) inside the loop for any particular iteration

break Vs. continue Example 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 above JavaScript example on break Vs. continue statement:

break vs continue javascript

JavaScript Online Test


« Previous Tutorial Next Tutorial »