javascriptoperatorsoperator-precedencepost-increment

Assignment along with Post increment


I am a bit confused with the output. Tried in Javascript

var x = 1;
x = x++;
console.log(x); //Its output is 1

I was thinking it to be 2. because I am doing the print after the post-increment. Any views on it?


Solution

  • The order in which x = x++ is executed is as follows:

    The above rules are described here. The rules indicate that x is incremented before assignment, not after.