標籤語句
試一試
let i, j;
loop1: for (i = 0; i < 3; i++) {
loop2: for (j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
break loop1;
}
console.log(`i = ${i}, j = ${j}`);
}
}
// Expected output:
// "i = 0, j = 0"
// "i = 0, j = 1"
// "i = 0, j = 2"
// "i = 1, j = 0"
語法
label:
statement
描述
你可以使用標籤來標識一個語句,然後使用 break 或 continue 語句引用它。請注意,JavaScript 沒有 goto 語句;你只能將標籤與 break 或 continue 一起使用。
任何引用 label 的 break 或 continue 都必須包含在由 label 標記的 statement 中。可以將 label 視為一個僅在 statement 作用域內可用的變數。
如果在執行 statement 時遇到 break label; 語句,則 statement 的執行終止,並在帶標籤語句之後的語句處繼續執行。
僅當 statement 是迴圈語句之一時,才能使用 continue label;。如果在執行 statement 時遇到 continue label; 語句,則 statement 的執行將在迴圈的下一次迭代中繼續。不帶標籤的 continue; 只能繼續最內層迴圈,而 continue label; 允許繼續任何給定迴圈,即使該語句巢狀在其他迴圈中。
一個語句可以有多個標籤。在這種情況下,所有標籤在功能上都是等效的。
示例
在 for 迴圈中使用帶標籤的 continue
// The first for statement is labeled "loop1"
loop1: for (let i = 0; i < 3; i++) {
// The second for statement is labeled "loop2"
loop2: for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
continue loop1;
}
console.log(`i = ${i}, j = ${j}`);
}
}
// Logs:
// i = 0, j = 0
// i = 0, j = 1
// i = 0, j = 2
// i = 1, j = 0
// i = 2, j = 0
// i = 2, j = 1
// i = 2, j = 2
請注意它是如何跳過“i = 1, j = 1”和“i = 1, j = 2”的。
在 for 迴圈中使用帶標籤的 break
let i, j;
// The first for statement is labeled "loop1"
loop1: for (i = 0; i < 3; i++) {
// The second for statement is labeled "loop2"
loop2: for (j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
break loop1;
}
console.log(`i = ${i}, j = ${j}`);
}
}
// Logs:
// i = 0, j = 0
// i = 0, j = 1
// i = 0, j = 2
// i = 1, j = 0
請注意與上一個 continue 示例的區別:當遇到 break loop1 時,外部迴圈的執行終止,因此除了“i = 1, j = 0”之外不再有其他日誌;當遇到 continue loop1 時,外部迴圈的執行在下一次迭代中繼續,因此只跳過“i = 1, j = 1”和“i = 1, j = 2”。
使用帶標籤的 continue 語句
給定一個專案陣列和測試陣列,此示例計算透過所有測試的專案數。
// Numbers from 1 to 100
const items = Array.from({ length: 100 }, (_, i) => i + 1);
const tests = [
{ pass: (item) => item % 2 === 0 },
{ pass: (item) => item % 3 === 0 },
{ pass: (item) => item % 5 === 0 },
];
let itemsPassed = 0;
itemIteration: for (const item of items) {
for (const test of tests) {
if (!test.pass(item)) {
continue itemIteration;
}
}
itemsPassed++;
}
請注意 continue itemIteration; 語句如何跳過當前專案的所有剩餘測試以及更新 itemsPassed 計數器的語句,並繼續處理下一個專案。如果不使用標籤,則需要使用布林標誌代替。
// Numbers from 1 to 100
const items = Array.from({ length: 100 }, (_, i) => i + 1);
const tests = [
{ pass: (item) => item % 2 === 0 },
{ pass: (item) => item % 3 === 0 },
{ pass: (item) => item % 5 === 0 },
];
let itemsPassed = 0;
for (const item of items) {
let passed = true;
for (const test of tests) {
if (!test.pass(item)) {
passed = false;
break;
}
}
if (passed) {
itemsPassed++;
}
}
使用帶標籤的 break 語句
給定一個專案陣列和測試陣列,此示例確定所有專案是否都透過所有測試。
// Numbers from 1 to 100
const items = Array.from({ length: 100 }, (_, i) => i + 1);
const tests = [
{ pass: (item) => item % 2 === 0 },
{ pass: (item) => item % 3 === 0 },
{ pass: (item) => item % 5 === 0 },
];
let allPass = true;
itemIteration: for (const item of items) {
for (const test of tests) {
if (!test.pass(item)) {
allPass = false;
break itemIteration;
}
}
}
同樣,如果不使用標籤,則需要使用布林標誌代替。
// Numbers from 1 to 100
const items = Array.from({ length: 100 }, (_, i) => i + 1);
const tests = [
{ pass: (item) => item % 2 === 0 },
{ pass: (item) => item % 3 === 0 },
{ pass: (item) => item % 5 === 0 },
];
let allPass = true;
for (const item of items) {
let passed = true;
for (const test of tests) {
if (!test.pass(item)) {
passed = false;
break;
}
}
if (!passed) {
allPass = false;
break;
}
}
使用帶標籤的塊與 break
你可以標記迴圈以外的語句,例如簡單的塊,但只有 break 語句可以引用非迴圈標籤。
foo: {
console.log("face");
break foo;
console.log("this will not be executed");
}
console.log("swap");
// Logs:
// "face"
// "swap"
帶標籤的函式宣告
標籤只能應用於語句,而不是宣告。有一箇舊語法允許在非嚴格程式碼中標記函式宣告
L: function F() {}
但是,在嚴格模式程式碼中,這會丟擲SyntaxError
"use strict";
L: function F() {}
// SyntaxError: functions cannot be labelled
非普通函式,例如生成器函式和非同步函式,既不能在嚴格程式碼中標記,也不能在非嚴格程式碼中標記
L: function* F() {}
// SyntaxError: generator functions cannot be labelled
帶標籤的函式宣告語法已棄用,即使在非嚴格程式碼中也不應使用。你實際上無法在函式體中跳轉到此標籤。
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-labelled-statements |
瀏覽器相容性
載入中…