I have an example at https://jsfiddle.net/xfmkgurv/ where the prefix increment operation increments a variable's value then that variable's value is passed as an argument to a function. The code below is the same as in the fiddle.
function myFunc(val) {
alert(`Value is: ${val}`);
return val;
}
let y = 10;
const result = ++y + myFunc(y);
Operator precedence on MDN states that function calls have higher precedence than the prefix increment operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
If that is the case, I would expect that the statement: ++y + myFunc(y);
would first evaluate the expression in the myFunc
argument list, the variable y
, and that that would evaluate to 10
, then call the function myFunc
with that value, which would be assigned to the val
parameter in that function and that 10
would be alerted. 11
is alerted however, so it seems that the prefix increment operation happens to the variable y
before it is used in the function call.
Why is that when function calls have higher precedence?
Thanks!