JavaScript breakStatement

JavaScript 语句参考手册JavaScript Statement Reference

Examples

In this example, we used the break statement in a loop.

Loop through the code block and exit the loop when variable i is "3":

var text = ""
var i;
for (i = 0; i < 5; i++) {
    if (i == 3) {
        break;
    }
    text += "The number is " + i + "<br>";
}

textThe output is:

The number is 0
The number is 1
The number is 2

Try it Yourself »

More examples are included at the bottom of this article.


Definition and Usage

The break statement is used to exit a switch statement or loop statements (for, for...in, while, do...while).

When the break statement is used in a switch statement, it breaks out of the switch code block and terminates code execution.

When the break statement is used in a loop statement, it terminates the loop and executes the code after the loop (if any).

The break statement can also be used with an optional label reference to break out of a code block. (See "More Examples" below).

Note:The break statement (without a label reference) can only be used in a loop or switch.


Browser Support

Statement
break Yes Yes Yes Yes Yes


Syntax

break;

Using the optional label reference:

break labelname;

Technical Details

JavaScript Version: 1.0. JavaScript 1.2 supports optional labels.


实例

More Examples

Examples

This example uses the break statement in a while loop statement.

Loop through the code block and exit the loop when i equals "3":

var text = "";
var i = 0;
while (i < 5) {
    text += "<br>The number is " + i;
    i++;
    if (i == 3) {
        break;
    }
}

textThe output is:

The number is 0
The number is 1
The number is 2

Try it Yourself »

Examples

Break out of the switch statement block to ensure that only one case statement block executes:

var day;
switch (new Date().getDay()) {
    case 0:
        day = "Sunday";
        break;
    case 1:
        day = "Monday";
        break;
    case 2:
        day = "Tuesday";
        break;
    case 3:
        day = "Wednesday";
        break;
    case 4:
        day = "Thursday";
        break;
    case 5:
        day = "Friday";
        break;
    case 6:
        day = "Saturday";
        break;
}

dayThe output is:


Try it Yourself »

Examples

Using the break statement with a label reference to break out of a code block:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";

list: {
    text += cars[0] + "<br>";
    text += cars[1] + "<br>";
    text += cars[2] + "<br>";
    break list;
    text += cars[3] + "<br>";
}

textThe output is:

BMW
Volvo
Saab

Try it Yourself »

Examples

Using the break statement with a label reference to break out of nested loops:

var text = "";
var i, j;

Loop1: // first loop label "Loop1"
for (i = 0; i < 3; i++) {
text += "<br>" + "i = " + i + ", j = ";

Loop2: // second loop label "Loop2"
    for (j = 10; j < 15; j++) {
        if (j == 12) {
            break Loop2;
        }
        document.getElementById("demo").innerHTML = text += j + " ";
    }
}

textThe output is:

i = 0, j = 10 11
i = 1, j = 10 11
i = 2, j = 10 11

Try it Yourself »


Related Pages

JavaScript Tutorial:JavaScript Break and Continue

JavaScript Tutorial:JavaScript Loops

JavaScript Tutorial:JavaScript While Loop

JavaScript Tutorial:JavaScript continue Statement

JavaScript Reference:JavaScript for Statement

JavaScript Reference:JavaScript while Statement


JavaScript 语句参考手册JavaScript Statement Reference Other Extensions