The priority of AND && is higher than OR ||. That's why it works before OR.
In the example below, 1 && 0
is calculated first.
alert( 5 || 1 && 0 ); // 5
I don't understand how this happens?
CodePudding user response:
The left-hand side will be evaluated first. Please see example
a || (b * c); // evaluate `a` first, then produce `a` if `a` is "truthy"
a && (b < c); // evaluate `a` first, then produce `a` if `a` is "falsy"
a ?? (b || c); // evaluate `a` first, then produce `a` if `a` is not `null` and not `undefined`
a?.b.c; // evaluate `a` first, then produce `undefined` if `a` is `null` or `undefined`
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
CodePudding user response:
Please check Operator Precedence for more details.
Order is from left to right, when any of the early left part evaluates to be true, the rest of right side is not evaluated and is omitted.
console.log(5 || false); // 5
console.log(true || 5 || false); // true
console.log(false || 5 || 10); // 5
console.log(false || 10 || 5); // 10