Using the break statement - 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 - 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.  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>

Related Tutorials