Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139654

MDN states that function calls have higher operator precedence than the prefix increment operator, then please explain this

$
0
0

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!


Viewing all articles
Browse latest Browse all 139654

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>