Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

break Redirect 1

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

Document Tags and Contributors

 Contributors to this page: Sheppy
 Last updated by: Sheppy,