Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

break

BaselineWidely available

Thebreak statement terminates the current loop orswitch statement and transfers program control to the statement following the terminated statement. It can also be used to jump past alabeled statement when used within that labeled statement.

Try it

let i = 0;while (i < 6) {  if (i === 3) {    break;  }  i += 1;}console.log(i);// Expected output: 3

Syntax

js
break;break label;
labelOptional

Identifier associated with the label of the statement to break to. If thebreak statement is not nested within a loop orswitch, then the label identifier is required.

Description

Whenbreak; is encountered, the program breaks out of the innermostswitch orlooping statement and continues executing the next statement after that.

Whenbreak label; is encountered, the program breaks out of the statement labeled withlabel and continues executing the next statement after that. Thebreak statement needs to be nested within the referenced label. The labeled statement can be any statement (commonly ablock statement); it does not have to be another loop statement.

Abreak statement, with or without a following label, cannot be used at the top level of a script, module, function's body, orstatic initialization block, even when the function or class is further contained within a loop.

Examples

break in while loop

The following function has abreak statement that terminates thewhile loop wheni is 3, and then returns the value3 * x.

js
function testBreak(x) {  let i = 0;  while (i < 6) {    if (i === 3) {      break;    }    i += 1;  }  return i * x;}

break in switch statements

The following code has abreak statement that terminates theswitch statement when a case is matched and the corresponding code has run.

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

break in labeled blocks

The following code usesbreak statements with labeled blocks. By usingbreak outerBlock, control is transferred to the end of the block statement marked asouterBlock.

js
outerBlock: {  innerBlock: {    console.log("1");    break outerBlock; // breaks out of both innerBlock and outerBlock    console.log(":-("); // skipped  }  console.log("2"); // skipped}

Unsyntactic break statements

Abreak statement must be nested within any label it references. The following code also usesbreak statements with labeled blocks, but generates a syntax error because itsbreak statement referencesblock2 but it's not nested withinblock2.

js
block1: {  console.log("1");  break block2; // SyntaxError: label not found}block2: {  console.log("2");}

Syntax errors are also generated in the following code examples which usebreak statements within functions that are nested within a loop, or labeled block that thebreak statements are intended to break out of.

js
function testBreak(x) {  let i = 0;  while (i < 6) {    if (i === 3) {      (() => {        break;      })();    }    i += 1;  }  return i * x;}testBreak(1); // SyntaxError: Illegal break statement
js
block1: {  console.log("1");  (() => {    break block1; // SyntaxError: Undefined label 'block1'  })();}

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-break-statement

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp