Now we go out of the basics of incrementing. When you put
myVar++ as part of a bigger expression (like in
console.log(myVar++)), two things happen in order:
- The current value of
myVar is used. That’s why you see
32 in the output.
- The value is increased by 1.
myVar's value is now 33.
Why don’t you run it and see for yourself?
There is another variation of the increment operator: placing the
++ in front of the variable (like in
++alfa). When used as its own statement (aka the basic usage), it’s no different from
alfa++. But when part of a larger expression, the value is incremented first, then the updated value is used.
bita = ++alfa; // bita will be 11 because alfa is incremented first.
As for the last line, it’s the same reasoning as the first example. Since by the time the code hits the
alfa's value is now 11. It will first print 11 then increment, then print 12 and increment again.
Tip. Next time, make a new thread to ask a new question, instead of editing an existing one. Your edit might go unnoticed for a long time and you can’t really expect it to be noticed by other users. Just don’t make multiple threads for the exact same question