9

What is the correct way to interpret this complicated 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

4
  • 1
    Uh oh, better break out the spec again...
    – zzzzBov
    Apr 2, 2012 at 15:47
  • That is because you haven't give brackets. Try giving brackets. It should work as expected.
    – sgowd
    Apr 2, 2012 at 15:49
  • not really.. inline if does not require brackets or parenthesis. Check Simon West's answer below. Apr 2, 2012 at 15:51
  • 1
    It's interpreted as some_condition ? (a = b) : (c = d = e);
    – gen_Eric
    Apr 2, 2012 at 15:52

6 Answers 6

3

If 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);
0
2

Thats an abbreviated if else statement

the long form would be

if(some condition)
{
 a = b;
}
else 
{
 c = d = e;
}
2
  • 1
    I'm pretty certain @tyty understands what the ?: operator does, the question was about operator precedence.
    – zzzzBov
    Apr 2, 2012 at 15:51
  • 3
    @zzzzBov then perhaps he should rephrase the question. The way hes throwing around the perenthesis in his question make it look like he dosn't actually realise what it is.
    – Simon West
    Apr 2, 2012 at 15:56
0
if( some_condition ){
    a=b;
}else{
    d=e;
    c=d;
}
8
  • Also effectively the same as d = e and c = e for that last bit.
    – kiswa
    Apr 2, 2012 at 15:50
  • d=e; followed by c=d; is not technically correct, as d=e; returns the value of e
    – zzzzBov
    Apr 2, 2012 at 15:50
  • More importantly the -1 was because this doesn't answer the question in any way.
    – zzzzBov
    Apr 2, 2012 at 15:50
  • So does d since it got assigned the value of e. Don't be pedantic.
    – Creynders
    Apr 2, 2012 at 15:57
  • 1
    Yeah, but normally you stick to the scope of the problem presented, which isn't about mutators nor accessors. It's not even about members, so your point is moot I'm afraid. IF the question would've used foo.bar, I'd answered differently. It did not. Maybe you're right and he does know what the ternary operator does, but it's not clear from his question, that's why I chose to answer it as simple as possible. Since c=e isn't pedantically technically correct either, I chose to sacrifice technical absolute correctness for readability. That's what you do when teaching or answering questions.
    – Creynders
    Apr 2, 2012 at 16:05
0

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

0

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.

0

The Conditional Operator is defined as:

ConditionalExpression :
    LogicalORExpression
    LogicalORExpression ? AssignmentExpression : AssignmentExpression

a = b is an AssignmentExpression, and so is c = d = e.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.