Use a while loop together with the break statement. - Javascript Statement

Javascript examples for Statement:for

Description

Use a while loop together with the break statement.

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials