Reputation: 2234
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?
Upvotes: 2
Views: 586
Reputation: 272096
The order in which x = x++
is executed is as follows:
x++
The above rules are described here. The rules indicate that x
is incremented before assignment, not after.
Upvotes: 7
Reputation: 6637
It's correct. The assignment goes first, then the incrementing. Compare:
var x = 1
var y = 1
x = x++
y = ++y
console.log(x, y)
Upvotes: 6