What is continue statement in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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 −


Live Demo

         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 /> ");
Published on 19-Jan-2018 10:51:03