ECMAScript® 2024 Language Specification

Draft ECMA-262 / February 15, 2024

14.13 Labelled Statements

Syntax

LabelledStatement[Yield, Await, Return] : LabelIdentifier[?Yield, ?Await] : LabelledItem[?Yield, ?Await, ?Return] LabelledItem[Yield, Await, Return] : Statement[?Yield, ?Await, ?Return] FunctionDeclaration[?Yield, ?Await, ~Default] Note

A Statement may be prefixed by a label. Labelled statements are only used in conjunction with labelled break and continue statements. ECMAScript has no goto statement. A Statement can be part of a LabelledStatement, which itself can be part of a LabelledStatement, and so on. The labels introduced this way are collectively referred to as the “current label set” when describing the semantics of individual statements.

14.13.1 Static Semantics: Early Errors

LabelledItem : FunctionDeclaration
  • It is a Syntax Error if any source text is matched by this production.
Note

An alternative definition for this rule is provided in B.3.1.

14.13.2 Static Semantics: IsLabelledFunction ( stmt )

The abstract operation IsLabelledFunction takes argument stmt (a Statement Parse Node) and returns a Boolean. It performs the following steps when called:

  1. If stmt is not a LabelledStatement, return false.
  2. Let item be the LabelledItem of stmt.
  3. If item is LabelledItem : FunctionDeclaration , return true.
  4. Let subStmt be the Statement of item.
  5. Return IsLabelledFunction(subStmt).

14.13.3 Runtime Semantics: Evaluation

LabelledStatement : LabelIdentifier : LabelledItem
  1. Return ? LabelledEvaluation of this LabelledStatement with argument « ».

14.13.4 Runtime Semantics: LabelledEvaluation

The syntax-directed operation LabelledEvaluation takes argument labelSet (a List of Strings) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. It is defined piecewise over the following productions:

BreakableStatement : IterationStatement
  1. Let stmtResult be Completion(LoopEvaluation of IterationStatement with argument labelSet).
  2. If stmtResult is a break completion, then
    1. If stmtResult.[[Target]] is empty, then
      1. If stmtResult.[[Value]] is empty, set stmtResult to NormalCompletion(undefined).
      2. Else, set stmtResult to NormalCompletion(stmtResult.[[Value]]).
  3. Return ? stmtResult.
BreakableStatement : SwitchStatement
  1. Let stmtResult be Completion(Evaluation of SwitchStatement).
  2. If stmtResult is a break completion, then
    1. If stmtResult.[[Target]] is empty, then
      1. If stmtResult.[[Value]] is empty, set stmtResult to NormalCompletion(undefined).
      2. Else, set stmtResult to NormalCompletion(stmtResult.[[Value]]).
  3. Return ? stmtResult.
Note 1

A BreakableStatement is one that can be exited via an unlabelled BreakStatement.

LabelledStatement : LabelIdentifier : LabelledItem
  1. Let label be the StringValue of LabelIdentifier.
  2. Let newLabelSet be the list-concatenation of labelSet and « label ».
  3. Let stmtResult be Completion(LabelledEvaluation of LabelledItem with argument newLabelSet).
  4. If stmtResult is a break completion and stmtResult.[[Target]] is label, then
    1. Set stmtResult to NormalCompletion(stmtResult.[[Value]]).
  5. Return ? stmtResult.
LabelledItem : FunctionDeclaration
  1. Return ? Evaluation of FunctionDeclaration.
Statement : BlockStatement VariableStatement EmptyStatement ExpressionStatement IfStatement ContinueStatement BreakStatement ReturnStatement WithStatement ThrowStatement TryStatement DebuggerStatement
  1. Return ? Evaluation of Statement.
Note 2

The only two productions of Statement which have special semantics for LabelledEvaluation are BreakableStatement and LabelledStatement.