大网数据广告活动
Web 标准与前端开发

SyntaxError: unlabeled break must be inside loop or switch

· 52CE 我爱测

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

The JavaScript exception "unlabeled break must be inside loop or switch" occurs when a Statements/break statement is not inside a loop or a Statements/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?

Statements/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 cannot be used outside switch or loops.

let score = 0;

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

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

let score = 0;

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

Using break in callbacks

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

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

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

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

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

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

See also

  • Statements/break

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。