# label

## 語法

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

`statement`

## 範例

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

```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` 陳述式

```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

```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` 陳述式

```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`

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

// this will log:

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

### 標記的函式宣告式

`L: function F() {}`

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

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

## 規格

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.2
ECMAScript 5.1 (ECMA-262)
The definition of 'Labelled statement' in that specification.
Standard
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Labelled statement' in that specification.
Standard
ECMAScript (ECMA-262)
The definition of 'Labelled statement' in that specification.
Living Standard

## 瀏覽器相容性

Update compatibility data on GitHub
Desktop Mobile Server Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 4 Opera Full support 4 Safari Full support 1 WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support 10.1 Safari iOS Full support 1 Samsung Internet Android Full support 1.0 nodejs Full support 0.1.100

Full support
Full support