Using the break statement with a label reference, to "jump out" of a nested for loop: - Javascript Statement

Javascript examples for Statement:break

Description

Using the break statement with a label reference, to "jump out" of a nested for loop:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//www  . j a  va  2 s  .c o m
    var text = "";
    var i, j;

    Loop1:               // The first for loop is labeled "Loop1"
    for (i = 0; i < 3; i++) {
    text += "<br>" + "i = " + i + ", j = ";
        Loop2:           // The second for loop is labeled "Loop2"
        for (j = 10; j < 15; j++) {
            if (j === 12) {
                 break Loop2;
            }
            document.getElementById("demo").innerHTML = text += j + " ";
        }
    }
}
</script>

</body>
</html>

Related Tutorials