break

Did you know that you can read content offline by using one of these tools? If you would like to read offline MDN content in another format, let us know by commenting on Bug 665750.

Dash App

Summary

Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.

Statement
Implemented in JavaScript 1.0, NES 2.0
ECMAScript Edition ECMA-262 (for the unlabeled version)
ECMA-262, Edition 3 (for the labeled version)

Syntax

break [label];

Parameters

label
Identifier associated with the label of the statement.  If the statement is not a loop or switch, this is required.

Description

The break statement includes an optional label that allows the program to break out of a labeled statement. The break statement needs to be nested within this labelled statement. The labelled statement can be any block statement; it does not have to be preceded by a loop statement.

Example

The following function has a break statement that terminates the while loop when i is 3, and then returns the value 3 * x.

function testBreak(x) {
   var i = 0;

   while (i < 6) {
      if (i == 3) {
         break;
      }
      i += 1;
   }
   return i * x;
}

See also

Tags (1)