1

Before I had an idea that inner most groups(regardless of explicit or implicit) in an expression executed first. But as I tested the following code, I've found that I was wrong:

function e(returnValue, name) {
  console.log(name);
  return returnValue;
}


(1 + (e(1, 'b') + (e(1, 'a') + 1)) + 1);
//Output is:
//b
//a


// Though there is no grouping below explicitly, but there are implicit grouping where the inner most expression is on the right hand side(note that it's short circuit operator and right associative)
false ? e('dog', 'D') :
true ? e('cat', 'C') :
true ? e('monkey', 'M') : e('goat', 'G');
// Output is:
// C 


// && has higher precedence so it implicitly groups it's operands and both are short circuit operators
e(false, 'a') || e(false, 'b') && e(true, 'c');
//output is:
//a
//b

Here all the sub expressions are executed left to right, not from highest to lowest precedence. This is what I've found by experimentation. But I'm not absolutely sure that it is true for all cases. I've not found any documentation about this. So in which order JavaScript executes sub expressions of an expression actually? It would be a great help if you also refer to some reliable resource.

  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence – Mister Jojo Feb 23 '20 at 02:45
  • I've gone through this page before. It doesn't talk about the execution order of sub expressions. – Ashutosh Biswas Feb 23 '20 at 02:51
  • and what about "Function Call" precedence=20 ? (for exemple) – Mister Jojo Feb 23 '20 at 03:02
  • 1
    I'm not sure what you're asking. As long as operator precedence is maintained it isn't really relevant what order expressions are evaluated in. In the last example the first expression will be evaluated first (a) then the && group, which short-circuits hence no (c). In the ternary I don't know why you'd expect anything different--there's no need to evaluate anything else. – Dave Newton Feb 23 '20 at 03:06
  • 1
    @MisterJojo The page says "Associativity determines how operators of the same precedence are parsed.". But my problem is in evaluation. I do not understand the function call operator clearly. Can you explain it a bit more please especially about it's operands? But the answer you have suggested solved my problem. Though I don't understand the official doc references. It's seems very complicated. – Ashutosh Biswas Feb 23 '20 at 09:43
  • @DaveNewton If evaluation order was different different outcome could be found. For example if the evaluation order was right to left then outcome of the last expression `e(false, 'a') || e(false, 'b') && e(true, 'c');` would be `b` cause `&&` have higher precedence. – Ashutosh Biswas Feb 23 '20 at 10:03
  • @DaveNewton Evaluation order doesn't matter only as long as the code is pure. As soon as the expressions have side effects though, it becomes relevant. In `(1+2) & (2+2)` it doesn't matter whether `1+2` or `2+2` will come first, the result is always the same. But with `x = ''; function a() { x='a'; return 3 } function b() { x='b'; return 4 }`, the evaluation in `a() & b()` matters a lot - even if the result of the expression is always the same, the value of `x` afterwards wouldn't. – Bergi Feb 23 '20 at 15:58
  • @AshutoshBiswas Yes, as you found out, JavaScript is always evaluated left-to-right. – Bergi Feb 23 '20 at 15:59

0 Answers0