Which Logic Operator Takes Precedence
My rule of thumb, which covers basically 99% of all use cases for conditional statements, is:
- Grouping:
()
- Member access
. or [...]
- Not:
!
- Comparison, e.g.
< , >= , === , !=, ...
- Logical AND
&&
- Logical OR
||
MDN gives you the exhaustive breakdown: Javascript Operator Precedence
so for your example:
(firstRun == true || selectedCategory != undefined && selectedState != undefined)
equals
(firstRun == true) || ((selectedCategory != undefined) && (selectedState != undefined))
For anything more complex than the above mentioned cases I would look into refactoring the code for readabilities sake anyways!
There is a pretty good rule of thumb to this. Think of these operators as of mathematical ones:
-
AND
is multiplication (eg.0 * 1 = 0 => FALSE
) -
OR
is adding (eg.0 + 1 = 1 => TRUE
)
When you remember this, all you have to know is that multiplication always comes before addition.