'Can someone explain me why "operator precedence" applies to logical operators like "||", "&&" in javaScript
Can someone explain me why operator precedence applies to logical operators like || and && in JavaScript? What does that mean in an operation like:
true || false && false
the false && false is evaluated first because the && operator is having a higher precedence than the || operator in JavaScript. according to how I know the false && false is not evaluated by the JavaScript engine because before the || operator there is a true literal and when something is true before the || operator the thing after the || operator will not be evaluated this is called "short-circuiting of logical operators" in JavaScript another example will be:
true || alert()
the function call never takes place even though the function call is having higher precedence than the || operator and another example is
true || x = 7
if short-circuiting of logical operators is true in JavaScript then the above code must not give an error because the x = 7 is not evaluated, since before the || operator there is a true literal.
Solution 1:[1]
Operator precedence just determines grouping, not actual evaluation order: https://stackoverflow.com/a/46506130
true || false && falsebecomestrue || (false && false)but is still evaluated from left to right.true || alert()is evaluated astrue || (alert())and NOT(true || alert)()true || x = 7is evaluated as(true || x) = 7and causes an error, NOTtrue || (x = 7)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mingwei Samuel |
