Increment a Number with JavaScriptn

Increment a Number with JavaScriptn
0

#1

Tell us what’s happening:

Your code so far


var myVar = 87;

// Only change code below this line
myVar = myVar++;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/increment-a-number-with-javascript


#2

@Aanwar,
in order to understand the code you have written, it is important to understand the post-increment and pre-increment operations.

assuming you have a variable called myVar, performing;

myVar++;

will increment the value of myVar after it’s assignment. Meaning, that if before the line of code above the value of myVar was 0. After this code runs it will be 1.

On the opposite end,

++myVar;

increments the value of myVar and then executes it’s assignment. Meaning, the value will first increment to 1.


#3

From the instructions in the challenge:

You can easily increment or add one to a variable with the ++ operator.

i++;

is the equivalent of

i = i + 1;

Note
The entire line becomes i++; , eliminating the need for the equal sign.