Tutorial :Does operator precedence matter for && and == in javascrtipt?


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



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


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »