What is the correct way to interpret this plicated javascript expression?
some_condition ? a = b : c = d = e;
Following operator precedence rules, I would expect it to be:
(some_condition ? a = b : c) = d = e;
But it seems that the grouping is actually:
EDIT: (The original grouping was unclear. See below for update)
EDIT: some_condition ? a = b : (c = d = e);
Why is this so? (And no I did not write that code)
EDIT: This seems to suggest that in Javascript to say ?:
have higher precedence than =
is not entirely true. As a further example:
x = y ? a = b : c = d = e;
If ?:
have higher precedence than =
(as in C) then the grouping would be
x = ((y ? a = b : c) = (d = e));
but rather (from the answers) what we have is
x = (y ? a = b : (c = d = e));
The relative precedence of ?:
and =
seems to depend on where they appear in the expression
What is the correct way to interpret this plicated javascript expression?
some_condition ? a = b : c = d = e;
Following operator precedence rules, I would expect it to be:
(some_condition ? a = b : c) = d = e;
But it seems that the grouping is actually:
EDIT: (The original grouping was unclear. See below for update)
EDIT: some_condition ? a = b : (c = d = e);
Why is this so? (And no I did not write that code)
EDIT: This seems to suggest that in Javascript to say ?:
have higher precedence than =
is not entirely true. As a further example:
x = y ? a = b : c = d = e;
If ?:
have higher precedence than =
(as in C) then the grouping would be
x = ((y ? a = b : c) = (d = e));
but rather (from the answers) what we have is
x = (y ? a = b : (c = d = e));
The relative precedence of ?:
and =
seems to depend on where they appear in the expression
- 1 Uh oh, better break out the spec again... – zzzzBov Commented Apr 2, 2012 at 15:47
- That is because you haven't give brackets. Try giving brackets. It should work as expected. – sgowd Commented Apr 2, 2012 at 15:49
- not really.. inline if does not require brackets or parenthesis. Check Simon West's answer below. – MilkyWayJoe Commented Apr 2, 2012 at 15:51
-
1
It's interpreted as
some_condition ? (a = b) : (c = d = e);
– gen_Eric Commented Apr 2, 2012 at 15:52
6 Answers
Reset to default 3If you look at javascript precedence operators, assignments
have a lower precedence than conditional operator
but, according to ecmascript specs, Par. 11.12
11.12 Conditional Operator ( ? : )
Syntax
ConditionalExpression : LogicalORExpression
LogicalORExpression ? AssignmentExpression : AssignmentExpression
AssignmentExpression is evaluated as follows:
1. Let lref be the result of evaluating LogicalORExpression.
2. If ToBoolean(GetValue(lref)) is true, then
a. Let trueRef be the result of evaluating the first AssignmentExpression.
b. Return GetValue(trueRef).
...
So the conditional operator evaluates that code grouping every assignment expression and it works as everybody explained.
Maybe I could be wrong here, but I think that operators precedence are related instead to how js parse an expression like
a = (b < c)? b : c;
(and not when contained in the AssignmentExpressions) where in this scenario the assignment should have a lower precedence, so Javascript evaluates the whole statement as
a = ((b < c)? b : c);
Thats an abbreviated if else
statement
the long form would be
if(some condition)
{
a = b;
}
else
{
c = d = e;
}
if( some_condition ){
a=b;
}else{
d=e;
c=d;
}
var result = true ? a=b : c=d=e;
if it is true a is equals to b and result equals b
var result = false ? a=b : c=d=e;
if it is false d equals to e, c also equals to e and result is e
You may also find this definition of the ternary operator helpful.
Usually that would be used for assigning something based on the condition, but this one seems to just be changing a
or c
and d
based on the condition.
I frequently see things like val = isConditionTrue ? trueVal : falseVal;
Then val
can be either trueVal
or falseVal
based on isConditionTrue
.
The Conditional Operator is defined as:
ConditionalExpression :
LogicalORExpression
LogicalORExpression ? AssignmentExpression : AssignmentExpression
a = b
is an AssignmentExpression, and so is c = d = e
.