ECMAScript® 2024 Language Specification

Draft ECMA-262 / February 15, 2024

13.14 Conditional Operator ( ? : )

Syntax

ConditionalExpression[In, Yield, Await] : ShortCircuitExpression[?In, ?Yield, ?Await] ShortCircuitExpression[?In, ?Yield, ?Await] ? AssignmentExpression[+In, ?Yield, ?Await] : AssignmentExpression[?In, ?Yield, ?Await] Note

The grammar for a ConditionalExpression in ECMAScript is slightly different from that in C and Java, which each allow the second subexpression to be an Expression but restrict the third expression to be a ConditionalExpression. The motivation for this difference in ECMAScript is to allow an assignment expression to be governed by either arm of a conditional and to eliminate the confusing and fairly useless case of a comma expression as the centre expression.

13.14.1 Runtime Semantics: Evaluation

ConditionalExpression : ShortCircuitExpression ? AssignmentExpression : AssignmentExpression
  1. Let lref be ? Evaluation of ShortCircuitExpression.
  2. Let lval be ToBoolean(? GetValue(lref)).
  3. If lval is true, then
    1. Let trueRef be ? Evaluation of the first AssignmentExpression.
    2. Return ? GetValue(trueRef).
  4. Else,
    1. Let falseRef be ? Evaluation of the second AssignmentExpression.
    2. Return ? GetValue(falseRef).