Which Logic Operator Takes Precedence

My rule of thumb, which covers basically 99% of all use cases for conditional statements, is:

  1. Grouping: ()
  2. Member access . or [...]
  3. Not: !
  4. Comparison, e.g. < , >= , === , !=, ...
  5. Logical AND &&
  6. 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!

Leave a Comment