myVar = myVar++ doesn't return the same value as myVar = myVar + 1?

In Increment a Number with JavaScript, myVar = myVar++ doesn’t return the same value as myVar = myVar+ 1.

Even though how humans understand it, it is equivalent. Could anyone explain to me why that is?

I know I just have to write myVar++.

It helps if you use the Ask For Help button so we get your code and a link to the challenge for context.


They are completely different expressions.

myVar++ is a shorthand way to write myVar = myVar + 1 or myVar += 1. It says ‘add one to the current value of myVar’.

Calling myVar = myVar++ gets into the difference between prefix and postfix notation and is best avoided. That says, ‘get the current value of myVar, increment the value of myVar, then set myVar equal to the old value of myVar’.

1 Like

That’s hilarious. Thankyou LOL

Read more about prefix vs postfix here:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.