Javascript examples for Statement:break
Using the break statement - 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. j ava2 s . c o m var text = "" var i; for (i = 0; i < 5; i++) { if (i === 3) { break; } text += "The number is " + i + "<br>"; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>