Javascript examples for Statement:for
Use a while loop together with the break statement.
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/*from w w w .j a v a 2 s.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>