语法错误:未标记的中断必须位于循环或开关内部

break 语句不在循环或 switch 语句内时,会发生 JavaScript 异常 "未标记的中断必须位于循环或开关内部"。

¥The JavaScript exception "unlabeled break must be inside loop or switch" occurs when a break statement is not inside a loop or a switch statement.

信息

¥Message

SyntaxError: Illegal break statement (V8-based)
SyntaxError: unlabeled break must be inside loop or switch (Firefox)
SyntaxError: 'break' is only valid inside a switch or loop statement. (Safari)

错误类型

¥Error type

SyntaxError

什么地方出了错?

¥What went wrong?

break 语句可用于退出循环或 switch 语句,在其他地方使用它们会出现语法错误。或者,你可以向 break 语句提供 label,以打破带有该标签的任何语句 - 但是,如果该标签未引用包含语句,则会抛出另一个错误 语法错误:未找到标签

¥break statements can be used to exit a loop or a switch statement, and using them elsewhere is a syntax error. Alternatively, you can provide a label to the break statement to break out of any statement with that label — however, if the label does not reference a containing statement, another error SyntaxError: label not found will be thrown.

示例

¥Examples

非语法中断

¥Unsyntactic break

break 不能在 switch 或循环之外使用。

¥break cannot be used outside switch or loops.

js
let score = 0;

function increment() {
  if (score === 100)
    break; // SyntaxError: unlabeled break must be inside loop or switch
  }
  score++;
}

也许你打算使用 return 来提前终止函数,而不是 break

¥Maybe instead of break, you intend to use return to early-terminate a function.

js
let score = 0;

function increment() {
  if (score === 100) {
    return;
  }
  score++;
}

在回调中使用 break

¥Using break in callbacks

break 不能在回调中使用,即使回调是从循环中调用的。

¥break cannot be used in callbacks, even if the callback is called from a loop.

js
let containingIndex = 0;
const matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

while (containingIndex < matrix.length) {
  matrix[containingIndex].forEach((value) => {
    if (value === 5) {
      break; // SyntaxError: unlabeled break must be inside loop or switch
    }
  });
  containingIndex++;
}

相反,请重构代码,以便在回调之外使用 break

¥Instead, refactor the code so the break is used outside the callback.

js
let containingIndex = 0;
const matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

outer: while (containingIndex < matrix.length) {
  for (const value of matrix[containingIndex]) {
    if (value === 5) {
      break outer;
    }
  }
  containingIndex++;
}
js
let containingIndex = 0;
const matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

while (containingIndex < matrix.length) {
  if (matrix[containingIndex].includes(5)) {
    break;
  }
  containingIndex++;
}

无法提前终止 forEach() 循环。你可以改用 some(),或将其转换为 for...of 循环。

¥There's no way to early-terminate a forEach() loop. You can use some() instead, or convert it to a for...of loop.

js
array.forEach((value) => {
  if (value === 5) {
    break; // SyntaxError: unlabeled break must be inside loop or switch
  }
  // do something with value
});
js
array.some((value) => {
  if (value === 5) {
    return true;
  }
  // do something with value
  return false;
});
js
for (const value of array) {
  if (value === 5) {
    break;
  }
  // do something with value
}

也可以看看

¥See also