Computer >> Computer tutorials >  >> Programming >> Javascript

How can I use a label with continue statement in JavaScript?


A label can be used to continue statement to control the flow more precisely in JavaScript. A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code.

Example

You can try to run the following code to learn how to work with labels with continue statement

Live Demo

<html>
   <body>
      <script>
         document.write("Entering the loop!<br /> ");
         
         outerloop: // This is the label name
            for (var i = 0; i < 3; i++) {
               document.write("Outerloop: " + i + "<br />");
               
               for (var j = 0; j < 5; j++) {
                  if (j == 3) {
                     continue outerloop;
                  }
                  document.write("Innerloop: " + j + "<br />");
               }
            }
            document.write("Exiting the loop!<br /> ");
      </script>
   </body>
</html>