3

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.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 2
    You have two parts, evaluation order, and precedence. The precedence says that for `true || false && false` it is equal to `true || (false && false)` and not `(true || false) && false`. You can see the precedence as how the tree out of the expression is built and then this tree is evaluated based on the evaluation order. – t.niese Apr 20 '20 at 09:53
  • 1
    so you mean what happens in here is totally an effect of the `"evaluation order"` of operators – user13104441 Apr 20 '20 at 12:15
  • 1
    Not the evaluation order of the operators, but the evaluation order of the expression. You have an expression with operator precedence that defines how the operators and operands are grouped. And you have the evaluation order which defines in which order those groupings defined by the operator precedence are evaluated, and if there is an early out in case that the already evaluated part will be sufficient to know the result of the expression. – t.niese Apr 20 '20 at 13:42
  • 1
    so what about an assignment expression like `x = 7` which operand is evaluated first – user13104441 Apr 20 '20 at 13:51
  • 1
    but if evaluation of operators is not the same as operator associativity the assignment `x = 7 ` must first evaluate `x` and later then it must evaluate `7` a good example is x = 5; x += x = 10; first the variable `x` which is before the `+=` is evaluated before the `x = 10` operation is executed – user13104441 Apr 20 '20 at 14:43
  • 1
    @t.niese are you sure though `x = x + (x = 10)` is equal to `x += x = 10` the first operation which is `x = x + (x = 10)` is what happens behind the scenes. because if the order of evaluation of operands is done in the order its written then the same rule should apply to assignment operators of javaScript the first operand should get evaluated first before the second operand another example is `x = 0; y = []; y[x] = x = 1;` – user13104441 Apr 20 '20 at 20:13
  • 2
    I will rewrite the comments as answer. – t.niese Apr 21 '20 at 09:04
  • where yu will rewrite the comments – user13104441 Apr 21 '20 at 09:08
  • 2
    Here, but I need some time. Due to the fact that, editing and combining comments is not possible, the whole thing is getting messy and misleading. – t.niese Apr 21 '20 at 09:13
  • 1
    take time friend its all right – user13104441 Apr 21 '20 at 10:21

1 Answers1

7

Operator precedence just determines grouping, not actual evaluation order: https://stackoverflow.com/a/46506130

  • true || false && false becomes true || (false && false) but is still evaluated from left to right.

  • true || alert() is evaluated as true || (alert()) and NOT (true || alert)()

  • true || x = 7 is evaluated as (true || x) = 7 and causes an error, NOT true || (x = 7)

Mingwei Samuel
  • 2,917
  • 1
  • 30
  • 40
  • 1
    but if the right operation of the `||` operator is having higher precedence why that operation is not evaluated first – user13104441 Apr 20 '20 at 10:08