Regarding the ternary (? :
) operator in JavaScript, I would like to know how it is evaluated by a typical browser's JavaScript interpreter:
Alternative A:
Alternative B:
Alternative C:
Of course, if neither alternative A nor alternative B accurately describe how the ternary operator works, please explain me how it works.
According to the specification it works like in Alternative A:
The production
ConditionalExpression : LogicalORExpression ? AssignmentExpression : AssignmentExpression
is evaluated as follows:
- Let
lref
be the result of evaluatingLogicalORExpression
.- If
ToBoolean(GetValue(lref))
istrue
, then
- Let
trueRef
be the result of evaluating the firstAssignmentExpression
.- Return
GetValue(trueRef)
.- Else
- Let
falseRef
be the result of evaluating the secondAssignmentExpression
.- Return
GetValue(falseRef)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With