What is continue statement in JavaScript?


The continue statement tells the interpreter to immediately start the next iteration of the loop and skip the remaining code block. The break statement is used to exit a loop early, breaking out of the enclosing curly braces.

When a continue statement is encountered, the program flow moves to the loop check expression immediately and if the condition remains true, then it starts the next iteration, otherwise, the control comes out of the loop.

You can try to run the following to learn how to work with continue statement in JavaScript. This example illustrates the use of a continue statement within a while loop. Notice how to continue statement is used to skip printing when the index held in variable x reaches 5 −

Example

Live Demo

<html>
   <body>
      <script>
         var x = 1;
         document.write("Entering the loop <br /> ");

         while (x < 10) {
            x = x+ 1;
            if (x== 5) {
               continue; // skip rest of the loop body
            }
            document.write( x + "<br />");
         }
         document.write("Exiting the loop!<br /> ");
      </script>
   </body>
</html>

Updated on: 13-Jun-2020

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements