break

Summary

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

Version Information

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version:

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;
}

Tags (0)

Edit tags
  • No tags

Attachments (0)

 

Attach file