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

With parameter input.

CaseBlock : { }
  1. Return NormalCompletion(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 the result of evaluating C.
      2. If R.[[Value]] is not empty, set V to R.[[Value]].
      3. If R is an abrupt completion, return Completion(UpdateEmpty(R, V)).
  5. Return NormalCompletion(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 « ».
  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 the result of evaluating C.
      2. If R.[[Value]] is not empty, set V to R.[[Value]].
      3. If R is an abrupt completion, return Completion(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 « ».
  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 the result of evaluating CaseClause C.
        2. If R.[[Value]] is not empty, set V to R.[[Value]].
        3. If R is an abrupt completion, return Completion(UpdateEmpty(R, V)).
  10. If foundInB is true, return NormalCompletion(V).
  11. Let R be the result of evaluating DefaultClause.
  12. If R.[[Value]] is not empty, set V to R.[[Value]].
  13. If R is an abrupt completion, return Completion(UpdateEmpty(R, 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 the result of evaluating CaseClause C.
    2. If R.[[Value]] is not empty, set V to R.[[Value]].
    3. If R is an abrupt completion, return Completion(UpdateEmpty(R, V)).
  16. Return NormalCompletion(V).

14.12.3 CaseClauseIsSelected ( C, input )

The abstract operation CaseClauseIsSelected takes arguments C (a Parse Node for CaseClause) and input (an ECMAScript language value). 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 the result of evaluating the Expression of C.
  3. Let clauseSelector be ? GetValue(exprRef).
  4. Return the result of performing Strict Equality Comparison 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 the result of evaluating 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 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 NormalCompletion(empty).
CaseClause : case Expression : StatementList
  1. Return the result of evaluating StatementList.
DefaultClause : default :
  1. Return NormalCompletion(empty).
DefaultClause : default : StatementList
  1. Return the result of evaluating StatementList.