I’m not sure what the tests are checking for under the hood to explain why myVar++ is working and ++myVar is not, but I just wanted to post quickly an example of the difference between the two:
++myVar is incrementing the variable and returning the new number. myVar++ returns the number before it was incrementing. Doing console.log(myVar) after the increments will log the same thing no matter which you use, so I have a feeling the code running the tests is checking what’s getting logged when the increment is happening in addition to the new value of myVar.
For example:
var num = 0;
if (num++ === 1) {
console.log('Num now equals 1'); // not going to get logged
}
// alternatively
var num = 0;
if (++num === 1) {
console.log('Num now equals 1'); // this is going to get logged
}
Also, note that the last form evaluates slightly later than the first three. So, always stick to the first three forms unless you know what you are doing.
For example, run this code and check the results:
var myVar1 = 0
var myVar2 = 0
console.log(++myVar1)
console.log(myVar2++)
console.log(myVar1, myVar2)