Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript break statement

JavaScript break statement

JavaScript Statement Reference JavaScript statement reference manual

Example

In this example we use the break statement inside a loop.

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

var text = ""

var i;

for (i = 0; i < 5; i++) {

    if (i == 3) {

        break;

    }

    text += "The number is " + i + "
";

}

text The output result is:

The number is 0

The number is 1

The number is 2

Try it »

More examples are included at the bottom of this article.


Definition and Usage

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

When the break statement is used in a switch statement, it jumps 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 optional label references to break out of a code block. (See the following "More examples".)

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


Browser support

Statement
break Yes Yes Yes Yes Yes

Syntax

break;

Use an optional label reference:

break

labelname

;

Technical Details

JavaScript version: 1.0. JavaScript 1.2 supports optional labels.

More examples

Example

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 += "
The number is " + i;

    i++;

    if (i == 3) {

        break;

    }

}

text The output is:

The number is 0

The number is 1

The number is 2

Try it »

Example

Break out of the switch statement block to ensure only one case block is executed:

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;

}

day The output result is:

Try it »

Example

Use 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] + "
";

    text += cars[1] + "
";

    text += cars[2] + "
";

    break list;

    text += cars[3] + "
";

}

text The output result is:

BMW

Volvo

Saab

Try it »

Example

Use 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 += "
" + "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 + " ";

    }

}

text The output result is:

i = 0, j = 10 11

i = 1, j = 10 11

i = 2, j = 10 11

Try it »


Related pages

JavaScript tutorial: JavaScript Break and Continue

JavaScript tutorial: JavaScript loops

JavaScript tutorial: JavaScript While Loop

JavaScript tutorial: JavaScript continue Statement

JavaScript Reference Manual: JavaScript for Statement

JavaScript Reference Manual: JavaScript while Statement


JavaScript Statement Reference JavaScript statement reference manual