# label

## 語法

```label :
statement
```
`label`

`statement`

## 範例

### 在 `for` 迴圈中使用帶標記的 `continue`

js
``````var i, j;

loop1: for (i = 0; i < 3; i++) {
//The first for statement is labeled "loop1"
loop2: for (j = 0; j < 3; j++) {
//The second for statement is labeled "loop2"
if (i === 1 && j === 1) {
continue loop1;
}
console.log("i = " + i + ", j = " + j);
}
}

// Output is:
//   "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"
// Notice how it skips both "i = 1, j = 1" and "i = 1, j = 2"
``````

### 使用帶標記的 `continue` 陳述式

js
``````var itemsPassed = 0;
var i, j;

top: for (i = 0; i < items.length; i++) {
for (j = 0; j < tests.length; j++) {
if (!tests[j].pass(items[i])) {
continue top;
}
}

itemsPassed++;
}
``````

### 在 `for` 迴圈中使用帶標記的 `break`

js
``````var i, j;

loop1: for (i = 0; i < 3; i++) {
//The first for statement is labeled "loop1"
loop2: for (j = 0; j < 3; j++) {
//The second for statement is labeled "loop2"
if (i === 1 && j === 1) {
break loop1;
}
console.log("i = " + i + ", j = " + j);
}
}

// Output is:
//   "i = 0, j = 0"
//   "i = 0, j = 1"
//   "i = 0, j = 2"
//   "i = 1, j = 0"
// Notice the difference with the previous continue example
``````

### 使用帶標記 `break` 陳述式

js
``````var allPass = true;
var i, j;

top: for (i = 0; items.length; i++)
for (j = 0; j < tests.length; i++)
if (!tests[j].pass(items[i])) {
allPass = false;
break top;
}
``````

### 在標記的區塊中使用 `break`

js
``````foo: {
console.log("face");
break foo;
console.log("this will not be executed");
}
console.log("swap");

// this will log:

// "face"
// "swap
``````

### 標記的函式宣告式

js
``````L: function F() {}
``````

js
``````"use strict";
L: function F() {}
// SyntaxError: functions cannot be labelled
``````

js
``````L: function* F() {}
// SyntaxError: generator functions cannot be labelled
``````

## 瀏覽器相容性

BCD tables only load in the browser