break

break 语句终止当前循环或 switch 语句,并将程序控制转移到终止语句后面的语句。当在带标签的语句中使用时,它还可以用于跳过 标签声明

¥The break statement terminates the current loop or switch statement and transfers program control to the statement following the terminated statement. It can also be used to jump past a labeled statement when used within that labeled statement.

Try it

语法

¥Syntax

js
break;
break label;
label Optional

与要中断的语句的标签关联的标识符。如果 break 语句未嵌套在循环或 switch 内,则需要标签标识符。

描述

¥Description

当遇到 break; 时,程序会跳出最里面的 switchlooping 语句,并继续执行后面的下一条语句。

¥When break; is encountered, the program breaks out of the innermost switch or looping statement and continues executing the next statement after that.

当遇到 break label; 时,程序会跳出标有 label 的语句,并继续执行后面的下一条语句。break 语句需要嵌套在引用的标签内。带标签的语句可以是任意语句(一般为 block 语句);它不必是另一个循环语句。

¥When break label; is encountered, the program breaks out of the statement labeled with label and continues executing the next statement after that. The break statement needs to be nested within the referenced label. The labeled statement can be any statement (commonly a block statement); it does not have to be another loop statement.

break 语句,无论带或不带以下标签,都不能在脚本、模块、函数体或 静态初始化块 的顶层使用,即使函数或类进一步包含在循环中也是如此。

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

示例

¥Examples

中断 while 循环

¥break in while loop

下面的函数有一条 break 语句,当 i 为 3 时终止 while 循环,然后返回值 3 * x

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

js
function testBreak(x) {
  let i = 0;

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

  return i * x;
}

switch 语句中的中断

¥break in switch statements

以下代码有一条 break 语句,当 case 匹配并且相应的代码已运行时,该语句会终止 switch 语句。

¥The following code has a break statement that terminates the switch 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

以下代码使用带有标记块的 break 语句。通过使用 break outerBlock,控制权将转移到标记为 outerBlock 的块语句的末尾。

¥The following code uses break statements with labeled blocks. By using break outerBlock, control is transferred to the end of the block statement marked as outerBlock.

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

不符合语法的 break 语句

¥Unsyntactic break statements

break 语句必须嵌套在它引用的任何标签内。以下代码也使用带标记块的 break 语句,但会生成语法错误,因为其 break 语句引用 block2 但未嵌套在 block2 中。

¥A break statement must be nested within any label it references. The following code also uses break statements with labeled blocks, but generates a syntax error because its break statement references block2 but it's not nested within block2.

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

block2: {
  console.log("2");
}

以下代码示例中也会生成语法错误,这些代码示例在嵌套在循环或 break 语句要突破的标记块内的函数中使用 break 语句。

¥Syntax errors 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.

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'
  })();
}

规范

Specification
ECMAScript Language Specification
# sec-break-statement

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also