What is break statement in JavaScript?

JavascriptWeb DevelopmentFront End Technology

The break statement is used to exit a loop early, breaking out of the enclosing curly braces.

You can try to run the following to learn how to work with break statement in JavaScript. The following example illustrates the use of a break statement with a while loop. Notice how the loop breaks out early once x reaches 5 and reaches to document.write (..) statement just below to the closing curly brace −


Live Demo

         var x = 1;
         document.write("Entering the loop <br/> ");
         while (x < 20) {
            if (x == 5) {
               break; // breaks out of loop completely
            x = x +1;
            document.write( x + "<br/>");
Published on 19-Jan-2018 10:48:49