# label

## 语法

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

`statement`
JavaScript 语句。`break` 可用于任何标记语句，而 `continue` 可用于循环标记语句。

## 示例

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

## 规范

ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.2
ECMAScript 5.1 (ECMA-262)
Labelled statement
Standard
ECMAScript 2015 (6th Edition, ECMA-262)
Labelled statement
Standard
ECMAScript (ECMA-262)
Labelled statement
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