Javascript examples for Statement:break
Using the break statement - while Loop through a block of code, but exit the loop when the variable i is equal to "3":
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from ww w.jav a2s. c o m var text = ""; var i = 0; while (i < 5) { text += "<br>The number is " + i; i++; if (i === 3) { break; } } document.getElementById("demo").innerHTML = text; } </script> </body> </html>