starting to learn JavaScript, and I am currently learning incrementations and decrement, is it possible to increment on your desired value? example: increment = 10
let earnedUsd = 10;
earnedUsd ++;
console.log(earnedUsd ++ = 10);
starting to learn JavaScript, and I am currently learning incrementations and decrement, is it possible to increment on your desired value? example: increment = 10
let earnedUsd = 10;
earnedUsd ++;
console.log(earnedUsd ++ = 10);
There are many ways to do that. Here are 3 examples to show the solutions. I recommend to use the 1st and 2nd way, 3rd way is not optimal.
//1st way
var a = 5;
a++
//2nd way
var b = 5;
b += 1;
//3rd way
var c = 5;
c = c + 1;
console.log(a)
console.log(b)
console.log(c)
let earnedUsd = 10;
console.log(earnedUsd++) // earnedUsd = earnedUsd + 1
console.log(earnedUsd += 10) // earnedUsd = earnedUsd + 10
It returns the new value, which you could (for example) put into another variable or output with console.log. In your case, you are simply incrementing earnedUsd
and not doing anything with the result. That is fine.
On its own, the following statement will simply increase the stored value of earnedUsd
by 10.
earnedUsd += 10
It returns the new value, so you can console.log
it if you want.
console.log(earnedUsd += 10)
This will display the new value. This pattern of writing code was common in C
, but we try to discourage it in Javascript because it can be easy to miss, when skim-reading the code, that you are actually changing the stored value of earnedUsd
.
earnedUsd ++= 10
is a syntax error. To add 1, just use ++
. To add 10, just use +=10
.