-2

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);
  • 3
    [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) + [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Andreas Aug 04 '21 at 13:23
  • `val += 10` is the same as `val = val + 10` but not really the same as `val++` ten times. – VLAZ Aug 04 '21 at 13:23
  • use += or -= operator. When you want to use ++, you have to use with loop. – Kirill Savik Aug 04 '21 at 13:24
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators – vanowm Aug 04 '21 at 13:26
  • Does this answer your question? [Increment and decrement](https://stackoverflow.com/questions/35474926/increment-and-decrement) – Ankit Tiwari Aug 04 '21 at 18:33

3 Answers3

0

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)
Nagy Szabolcs
  • 227
  • 1
  • 9
0
let earnedUsd = 10;



console.log(earnedUsd++) // earnedUsd = earnedUsd + 1
console.log(earnedUsd += 10) // earnedUsd = earnedUsd + 10

MDN Increment

MDN +=

Nick
  • 33
  • 7
0

earnedUsd++ increments the value by 1

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.

You have mis-spelled the second usage: you probably meant earnedUsd += 10

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.

ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26