Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mohamedkhallouq
GitHub Repository: mohamedkhallouq/content
Path: blob/main/files/en-us/web/javascript/reference/statements/label/index.md
6516 views
---
title: label slug: Web/JavaScript/Reference/Statements/label page-type: javascript-statement browser-compat: javascript.statements.label
---

{{jsSidebar("Statements")}}

The labeled statement can be used with {{jsxref("Statements/break", "break")}} or {{jsxref("Statements/continue", "continue")}} statements. It is prefixing a statement with an identifier which you can refer to.

{{EmbedInteractiveExample("pages/js/statement-label.html")}}

Syntax

label: statement
  • label

    • : Any JavaScript identifier that is not a reserved word.

  • statement

    • : A JavaScript statement. break can be used with any labeled statement, and continue can be used with looping labeled statements.

Description

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 {{jsxref("SyntaxError")}} (let is a reserved identifier).

Examples

Using a labeled continue with for loops

let i, j; // The first for statement is labeled "loop1" loop1: for (i = 0; i < 3; i++) { // The second for statement is labeled "loop2" loop2: for (j = 0; j < 3; j++) { if (i === 1 && j === 1) { continue loop1; } console.log(`i = ${i}, j = ${j}`); } } // Logs: // 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.

let itemsPassed = 0; let 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

let i, j; // The first for statement is labeled "loop1" loop1: for (i = 0; i < 3; i++) { // The second for statement is labeled "loop2" loop2: for (j = 0; j < 3; j++) { if (i === 1 && j === 1) { break loop1; } console.log(`i = ${i}, j = ${j}`); } } // Logs: // 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.

let allPass = true; let i, j; top: for (i = 0; i < items.length; i++) { for (j = 0; j < tests.length; j++) { 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

Labels can only be applied to statements, not declarations. Still, the Annex B: Additional ECMAScript Features for Web Browsers section defines a legacy grammar to standardize labeled function declarations in non-strict code.

L: function F() {}

In strict mode code, however, this will throw a {{jsxref("SyntaxError")}}:

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

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

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

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{jsxref("Statements/break", "break")}}

  • {{jsxref("Statements/continue", "continue")}}