when i use ++ operator in javascript, why I get the value at last is 100 not 101?
I want to know the detail of the ++ operator in javascript?
let value = 100;
value = value++;
console.log(value); // 100 why the value is 100 at last
>Solution :
Because when you have the assignment operator, the right-hand operand is evaluated first, then that value is assigned to the receiver on the left.¹
So here’s what happens in value = value++:
- Read the value of
value(100) and set it aside (since we’ll need it in a minute). - Increase the value of
valueby one (making it101). - Take the value from #1 (
100) as the result value of the right-hand operand. - Store that value in
value.
¹ In specification terms it’s slightly more complicated than that, but the details aren’t important here.