complicated javascript conditional expression
Asked Answered
C

6

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

Counterinsurgency answered 2/4, 2012 at 15:43 Comment(4)
Uh oh, better break out the spec again...More
That is because you haven't give brackets. Try giving brackets. It should work as expected.Microeconomics
not really.. inline if does not require brackets or parenthesis. Check Simon West's answer below.Servant
It's interpreted as some_condition ? (a = b) : (c = d = e);Melburn
B
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);
Beuthen answered 2/4, 2012 at 16:1 Comment(0)
C
2

Thats an abbreviated if else statement

the long form would be

if(some condition)
{
 a = b;
}
else 
{
 c = d = e;
}
Conics answered 2/4, 2012 at 15:48 Comment(2)
I'm pretty certain @Counterinsurgency understands what the ?: operator does, the question was about operator precedence.More
@More 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.Conics
T
0
if( some_condition ){
    a=b;
}else{
    d=e;
    c=d;
}
Tezel answered 2/4, 2012 at 15:48 Comment(8)
Also effectively the same as d = e and c = e for that last bit.Edwards
d=e; followed by c=d; is not technically correct, as d=e; returns the value of eMore
More importantly the -1 was because this doesn't answer the question in any way.More
So does d since it got assigned the value of e. Don't be pedantic.Tezel
@Creynders, actually d could easily be expanded to be foo.bar which could have an accessor/mutator and not return the same value of e. When answering questions about why a programming language works in a particular way, it is absolutely necessary to be pedantic.More
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.Tezel
You have absolutely no explanation in your answer, and your code is incorrect. That was the reason I downvoted you, and it continues to be the reason my downvote is on your question. If you fix those two issues, I would remove my downvote.More
I didn't deem it necessary to add an explanation since the answer is so simple. But maybe you're right, for some people everything needs to be spelled out minutely. And c=d=e is not only bad practice, but also confusing for people who don't know what it does, so it would be silly to promote bad coding practices isn't it? And I grew fond of your downvote, I'm keeping it. He's so cuddly.Tezel
D
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

Dramatic answered 2/4, 2012 at 15:54 Comment(0)
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.

Edwards answered 2/4, 2012 at 15:55 Comment(0)
M
0

The Conditional Operator is defined as:

ConditionalExpression :
    LogicalORExpression
    LogicalORExpression ? AssignmentExpression : AssignmentExpression

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

More answered 2/4, 2012 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.