The break
terminates the current loop, switch
, or label statement and transfers program control to the statement following the terminated statement.
The break
terminates the current loop, switch
, or label statement and transfers program control to the statement following the terminated statement.
break; break label;
label
Optional
Identifier associated with the label of the statement. If the statement is not a loop or switch
, this is required.
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 the referenced label. The labeled statement can be any block statement; it does not have to be preceded by a loop statement.
A break
statement, with or without a following label, cannot be used within the body of a function that is itself nested within the current loop, switch, or label statement that the break
statement is intended to break out of.
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) { let i = 0; while (i < 6) { if (i === 3) { break; } i += 1; } return i * x; }
The following code has a break
statement that terminates the switch
statement when a case is matched and the corresponding code has run.
const food = "sushi"; switch (food) { case "sushi": console.log("Sushi is originally from Japan."); break; case "pizza": console.log("Pizza is originally from Italy."); break; default: console.log("I have never heard of that dish."); break; }
The following code uses break
statements with labeled blocks. A break
statement must be nested within any label it references. Notice that innerBlock
is nested within outerBlock
.
outerBlock: { innerBlock: { console.log('1'); break outerBlock; // breaks out of both inner_block and outer_block console.log(':-('); // skipped } console.log('2'); // skipped }
The following code also uses break
statements with labeled blocks, but generates a SyntaxError
because its break
statement is within block1
but references block2
. A break
statement must always be nested within any label it references.
block1: { console.log('1'); break block2; // SyntaxError: label not found } block2: { console.log('2'); }
SyntaxError
s are also generated in the following code examples which use break
statements within functions that are nested within a loop, or labeled block that the break
statements are intended to break out of.
function testBreak(x) { let i = 0; while (i < 6) { if (i === 3) { (function () { break; })(); } i += 1; } return i * x; } testBreak(1); // SyntaxError: Illegal break statement
block_1: { console.log('1'); (function () { break block_1; // SyntaxError: Undefined label 'block_1' })(); }
Specification |
---|
ECMAScript Language Specification # sec-break-statement |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
break |
1 |
12 |
1 |
3 |
4 |
1 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
1.0 |
0.10.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break