Page 1 of 1

Logical && and || operator precedence is broken

Posted: Fri Apr 07, 2017 3:11 pm
by nfrasser
Not a big deal since you can always use parens, but just a heads up for people experiencing issues with this.

In Boolean algebra, the logical AND operator has higher order-of-operations precedence than logical OR. Therefore, the following two expressions should be equivalent:

Code: Select all


(true && true) || (true && false) // Result: true
true && true || true && false // Result: false in JSX (should be true)
In JSX, their results vary as indicated. The second expression is equivalent to this:

Code: Select all


((true && true) || true) && false
This does not follow logical order of operations. I confirmed that the two expressions both evaluate to true in other JavaScript engines.