MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-dev-survey

Terjemahan ini belum lengkap. Mohon bantu menerjemahkan artikel ini dari Bahasa Inggris.

The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.

Labeled loops or blocks are very uncommon. Oftentimes function calls can be used instead of loop jumps.

Syntax

label :
   statement
label
Semua Identifier Javascript yang bukan merupakan reserved word
statement
Statement break dapat digunakan dengan semua statement berlabel, dan statement continue .dapat digunakan dengan statement looping berlabel

Deskripsi

You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

Note that JavaScript has NO goto statement, you can only use labels with break or continue.

In strict mode code, you can't use "let" as a label name. It will throw a SyntaxError (let is a reserved identifier).

Examples

Using a labeled continue with for loops

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"

Using a labeled continue statement

Given an array of items and an array of tests, this example counts the number of items that passes all the tests.

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++;
}

Using a labeled break with for loops

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

Using a labeled break statement

Given an array of items and an array of tests, this example determines whether all items pass all tests.

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;
    }

Using a labeled block with break

You can use labels within simple blocks, but only break statements can make use of non-loop labels.

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

// this will log:

// "face"
// "swap 

Labeled function declarations

Starting with ECMAScript 2015, labeled function declarations are now standardized for non-strict code in the web compatibility annex of the specification.

L: function F() {}

In strict mode code, however, this will throw a SyntaxError:

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

Generator functions can't be labeled in neither strict nor in non-strict code:

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

Specifications

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

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

Tag Dokumen dan Kontributor

 Kontributor untuk laman ini: id4799
 Terakhir diperbarui oleh: id4799,