views:

139

answers:

3

More specifically, is there a set of values ( a, b and c) for which the operator precedence matters in the statement:

var value = (a && b == c);

(with the exception of NaN).

+2  A: 

The language is parsed such that your statement is the equivalent of (a && (b == c)). The equality operator will always run before &&, || and other logical operators. You can find the nitty-gritty details here.

rmeador
Yeah I understand that but I was trying to figure out if it made a difference in that specific example.
Ej
+2  A: 

Yup. == binds more tightly than &&, so what you have binds as

var val = a && ( b == c)

See here. So a==0, b==1 and c==0 is false, while (a&&b)==c is true.

(Fixed typo. Dammit.)

Charlie Martin
Ej
Dammit. Thanks.
Charlie Martin
+3  A: 

Yes

js> false && true == false
false
js> (false && true) == false
true

Since == has higher precedence than &&, the first is parsed as false && (true == false), which is equivalent to false && false, and thus evaluates to false. The second is equivalent to false == false, which is true

Brian Campbell
Awesome, thanks :) BTW can you recommend a js console?
Ej
I'm using spidermonkey http://www.mozilla.org/js/spidermonkey/ I don't use it for all that much, just testing the occasional expression like this.
Brian Campbell
firebug https://addons.mozilla.org/en-US/firefox/addon/1843 has a JS console
sysrqb
Brian Campbell