I learned this last week, possibly highlighting my non-classical programming training. I have never come across this in all my years of JavaScript and apparently it is pervasive in other languages such as Java as well.
Incremental Operator ++
Many times I have seen or used the following method for incrementing an integer count
var marky = 0; console.log(++marky); console.log(marky);
This increments the integer marky by 1. not rocket science. In the image below you can see that the two console.log entries always return the same value
What I learned yesterday was using the ++ after the variable name also increases the variable – but only AFTER the value has been evaluated…
From this image you can see that the marky++ value and the marky value are not the same. The console.log(marky++); returns the existing marky value and then increments it.
var marky = 0; console.log(marky++); console.log(marky);
I have to ask myself “why would anyone do that?”. Why would I want to return a variable value and THEN increase it’s value, seems a little odd to me. I guess someone thought it was a good idea…..
Always makes me think “I wonder what else I don’t know? Probably best not think about that too much”….
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
