break Statement

Terminates the current loop, or if in conjunction with a label, terminates the associated statement.

break [label];


  • label
    Optional. Specifies the label of the statement you are breaking from.


You typically use the break statement in switch statements and while, for,, or do...while loops. You most commonly use the label argument in switch statements, but it can be used in any statement, whether simple or compound.

Executing the break statement causes the program flow to exit the current loop or statement. Program flow resumes with the next statement immediately following the current loop or statement.

Example 1

In this example, the counter is set up to count from 1 to 99; however, the break statement terminates the loop after 14 counts.

var s = "";
for (var i = 1; i < 100; i+)
    if (i == 15)
    s += i + " ";

Example 2

In the following code, the break statement refers to the for loop that is preceded by the Inner: statement. When j is equal to 24, the break statement causes the program flow to exit that loop. The numbers 21 through 23 print on each line.

var s = "";

for (var i = 1; i <= 10; i+)
    s += "\n";
    s += "i: " + i;

    s += " j: ";

    for (var j = 21; j <= 30; j+)
        if (j == 24)
             break Inner;
        s += j + " ";


Version 1

See Also


continue Statement

do...while Statement

for Statement Statement

Labeled Statement

switch Statement

while Statement

Change History




July 2009

Modified examples.

Content bug fix.

March 2009

Modified examples.

Information enhancement.