Javascript examples for Statement:for
Loop through a block of code, 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() {// ww w . j av a 2 s.c om 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>