Labeled statement
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Alabeled statement is anystatement that is prefixed with an identifier. You can jump to this label using abreak
orcontinue
statement nested within the labeled statement.
Try it
let i, j;loop1: for (i = 0; i < 3; i++) { loop2: for (j = 0; j < 3; j++) { if (i === 1 && j === 1) { break loop1; } console.log(`i = ${i}, j = ${j}`); }}// Expected output:// "i = 0, j = 0"// "i = 0, j = 1"// "i = 0, j = 2"// "i = 1, j = 0"
Syntax
label: statement
label
Any JavaScriptidentifier that is not areserved word.
statement
A JavaScript statement.
break
can be used within any labeled statement, andcontinue
can be used within labeled looping statements.
Description
You can use a label to identify a statement, and later refer to it using abreak
orcontinue
statement. Note that JavaScript hasnogoto
statement; you can only use labels withbreak
orcontinue
.
Anybreak
orcontinue
that referenceslabel
must be contained within thestatement
that's labeled bylabel
. Think aboutlabel
as a variable that's only available in the scope ofstatement
.
If abreak label;
statement is encountered when executingstatement
, execution ofstatement
terminates, and execution continues at the statement immediately following the labeled statement.
continue label;
can only be used ifstatement
is one of thelooping statements. If acontinue label;
statement is encountered when executingstatement
, execution ofstatement
continues at the next iteration of the loop.continue;
without a label can only continue the innermost loop, whilecontinue label;
allows continuing any given loop even when the statement is nested within other loops.
A statement can have multiple labels. In this case, the labels are all functionally equivalent.
Examples
Using a labeled continue with for loops
// The first for statement is labeled "loop1"loop1: for (let i = 0; i < 3; i++) { // The second for statement is labeled "loop2" loop2: for (let 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 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 previouscontinue
example: whenbreak loop1
is encountered, the execution of the outer loop is terminated, so there are no further logs beyond "i = 1, j = 0"; whencontinue loop1
is encountered, the execution of the outer loop continues at the next iteration, so only "i = 1, j = 1" and "i = 1, j = 2" are skipped.
Using a labeled continue statement
Given an array of items and an array of tests, this example counts the number of items that pass all the tests.
// Numbers from 1 to 100const items = Array.from({ length: 100 }, (_, i) => i + 1);const tests = [ { pass: (item) => item % 2 === 0 }, { pass: (item) => item % 3 === 0 }, { pass: (item) => item % 5 === 0 },];let itemsPassed = 0;itemIteration: for (const item of items) { for (const test of tests) { if (!test.pass(item)) { continue itemIteration; } } itemsPassed++;}
Note how thecontinue itemIteration;
statement skips the rest of the tests for the current item as well as the statement that updates theitemsPassed
counter, and continues with the next item. If you don't use a label, you would need to use a boolean flag instead.
// Numbers from 1 to 100const items = Array.from({ length: 100 }, (_, i) => i + 1);const tests = [ { pass: (item) => item % 2 === 0 }, { pass: (item) => item % 3 === 0 }, { pass: (item) => item % 5 === 0 },];let itemsPassed = 0;for (const item of items) { let passed = true; for (const test of tests) { if (!test.pass(item)) { passed = false; break; } } if (passed) { itemsPassed++; }}
Using a labeled break statement
Given an array of items and an array of tests, this example determines whether all items pass all tests.
// Numbers from 1 to 100const items = Array.from({ length: 100 }, (_, i) => i + 1);const tests = [ { pass: (item) => item % 2 === 0 }, { pass: (item) => item % 3 === 0 }, { pass: (item) => item % 5 === 0 },];let allPass = true;itemIteration: for (const item of items) { for (const test of tests) { if (!test.pass(item)) { allPass = false; break itemIteration; } }}
Again, if you don't use a label, you would need to use a boolean flag instead.
// Numbers from 1 to 100const items = Array.from({ length: 100 }, (_, i) => i + 1);const tests = [ { pass: (item) => item % 2 === 0 }, { pass: (item) => item % 3 === 0 }, { pass: (item) => item % 5 === 0 },];let allPass = true;for (const item of items) { let passed = true; for (const test of tests) { if (!test.pass(item)) { passed = false; break; } } if (!passed) { allPass = false; break; }}
Using a labeled block with break
You can label statements other than loops, such as simple blocks, but onlybreak
statements can reference non-loop labels.
foo: { console.log("face"); break foo; console.log("this will not be executed");}console.log("swap");// Logs:// "face"// "swap"
Labeled function declarations
Labels can only be applied tostatements, not declarations. There is a legacy grammar that allows function declarations to be labeled in non-strict code:
L: function F() {}
Instrict mode code, however, this will throw aSyntaxError
:
"use strict";L: function F() {}// SyntaxError: functions cannot be labelled
Non-plain functions, such asgenerator functions andasync functions can neither be labeled in strict code, nor in non-strict code:
L: function* F() {}// SyntaxError: generator functions cannot be labelled
The labeled function declaration syntax isdeprecated and you should not use it, even in non-strict code. You cannot actually jump to this label within the function body.
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-labelled-statements |