Question

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).

Was it helpful?

Solution

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

OTHER TIPS

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.

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top