ECMAScript® 2024 Language Specification

Draft ECMA-262 / February 15, 2024

14.12 The switch Statement

Syntax

SwitchStatement[Yield, Await, Return] : switch ( Expression[+In, ?Yield, ?Await] ) CaseBlock[?Yield, ?Await, ?Return] CaseBlock[Yield, Await, Return] : { CaseClauses[?Yield, ?Await, ?Return]opt } { CaseClauses[?Yield, ?Await, ?Return]opt DefaultClause[?Yield, ?Await, ?Return] CaseClauses[?Yield, ?Await, ?Return]opt } CaseClauses[Yield, Await, Return] : CaseClause[?Yield, ?Await, ?Return] CaseClauses[?Yield, ?Await, ?Return] CaseClause[?Yield, ?Await, ?Return] CaseClause[Yield, Await, Return] : case Expression[+In, ?Yield, ?Await] : StatementList[?Yield, ?Await, ?Return]opt DefaultClause[Yield, Await, Return] : default : StatementList[?Yield, ?Await, ?Return]opt

14.12.1 Static Semantics: Early Errors

SwitchStatement : switch ( Expression ) CaseBlock

14.12.2 Runtime Semantics: CaseBlockEvaluation

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

CaseBlock : { }
  1. Return undefined.
CaseBlock : { CaseClauses }
  1. Let V be undefined.
  2. Let A be the List of CaseClause items in CaseClauses, in source text order.
  3. Let found be false.
  4. For each CaseClause C of A, do
    1. If found is false, then
      1. Set found to ? CaseClauseIsSelected(C, input).
    2. If found is true, then
      1. Let R be Completion(Evaluation of C).
      2. If R.[[Value]] is not empty, set V to R.[[Value]].
      3. If R is an abrupt completion, return ? UpdateEmpty(R, V).
  5. Return V.
CaseBlock : { CaseClausesopt DefaultClause CaseClausesopt }
  1. Let V be undefined.
  2. If the first CaseClauses is present, then
    1. Let A be the List of CaseClause items in the first CaseClauses, in source text order.
  3. Else,
    1. Let A be a new empty List.
  4. Let found be false.
  5. For each CaseClause C of A, do
    1. If found is false, then
      1. Set found to ? CaseClauseIsSelected(C, input).
    2. If found is true, then
      1. Let R be Completion(Evaluation of C).
      2. If R.[[Value]] is not empty, set V to R.[[Value]].
      3. If R is an abrupt completion, return ? UpdateEmpty(R, V).
  6. Let foundInB be false.
  7. If the second CaseClauses is present, then
    1. Let B be the List of CaseClause items in the second CaseClauses, in source text order.
  8. Else,
    1. Let B be a new empty List.
  9. If found is false, then
    1. For each CaseClause C of B, do
      1. If foundInB is false, then
        1. Set foundInB to ? CaseClauseIsSelected(C, input).
      2. If foundInB is true, then
        1. Let R be Completion(Evaluation of CaseClause C).
        2. If R.[[Value]] is not empty, set V to R.[[Value]].
        3. If R is an abrupt completion, return ? UpdateEmpty(R, V).
  10. If foundInB is true, return V.
  11. Let defaultR be Completion(Evaluation of DefaultClause).
  12. If defaultR.[[Value]] is not empty, set V to defaultR.[[Value]].
  13. If defaultR is an abrupt completion, return ? UpdateEmpty(defaultR, V).
  14. NOTE: The following is another complete iteration of the second CaseClauses.
  15. For each CaseClause C of B, do
    1. Let R be Completion(Evaluation of CaseClause C).
    2. If R.[[Value]] is not empty, set V to R.[[Value]].
    3. If R is an abrupt completion, return ? UpdateEmpty(R, V).
  16. Return V.

14.12.3 CaseClauseIsSelected ( C, input )

The abstract operation CaseClauseIsSelected takes arguments C (a CaseClause Parse Node) and input (an ECMAScript language value) and returns either a normal completion containing a Boolean or an abrupt completion. It determines whether C matches input. It performs the following steps when called:

  1. Assert: C is an instance of the production CaseClause : case Expression : StatementListopt .
  2. Let exprRef be ? Evaluation of the Expression of C.
  3. Let clauseSelector be ? GetValue(exprRef).
  4. Return IsStrictlyEqual(input, clauseSelector).
Note

This operation does not execute C's StatementList (if any). The CaseBlock algorithm uses its return value to determine which StatementList to start executing.

14.12.4 Runtime Semantics: Evaluation

SwitchStatement : switch ( Expression ) CaseBlock
  1. Let exprRef be ? Evaluation of Expression.
  2. Let switchValue be ? GetValue(exprRef).
  3. Let oldEnv be the running execution context's LexicalEnvironment.
  4. Let blockEnv be NewDeclarativeEnvironment(oldEnv).
  5. Perform BlockDeclarationInstantiation(CaseBlock, blockEnv).
  6. Set the running execution context's LexicalEnvironment to blockEnv.
  7. Let R be Completion(CaseBlockEvaluation of CaseBlock with argument switchValue).
  8. Set the running execution context's LexicalEnvironment to oldEnv.
  9. Return R.
Note

No matter how control leaves the SwitchStatement the LexicalEnvironment is always restored to its former state.

CaseClause : case Expression :
  1. Return empty.
CaseClause : case Expression : StatementList
  1. Return ? Evaluation of StatementList.
DefaultClause : default :
  1. Return empty.
DefaultClause : default : StatementList
  1. Return ? Evaluation of StatementList.