Using the break statement - while Loop through a block of code, but exit the loop when the variable i is equal to "3": - Javascript Statement

Javascript examples for Statement:break

Description

Using the break statement - while Loop through a block of code, but exit the loop when the variable i is equal to "3":

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

Related Tutorials