Path: blob/main/files/en-us/web/javascript/reference/statements/label/index.md
6516 views
------{{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: Any JavaScript identifier that is not a reserved word.
statement: A JavaScript statement.
breakcan be used with any labeled statement, andcontinuecan 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
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.
Using a labeled break with for loops
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.
Using a labeled block with break
You can use labels within simple blocks, but only break statements can make use of non-loop labels.
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.
In strict mode code, however, this will throw a {{jsxref("SyntaxError")}}:
Generator functions can neither be labeled in strict code, nor in non-strict code:
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
{{jsxref("Statements/break", "break")}}
{{jsxref("Statements/continue", "continue")}}