# 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 Latest Draft (ECMA-262)
The definition of 'Labelled statement' in that specification.
Draft

## 瀏覽器相容性

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

Full support
Full support