-6

Can someone help explain to me why is it that

let one = 1;

function chg(arg) {
  return arg++ // --> shouldn't this be similar to arg + arg? but it's outputting 1
}

console.log(one);
console.log(chg(one));
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Kimimomo
  • 99
  • 1
  • 8
  • 1
    If `one` is `1`. Then `one++` is `1` and `one` becomes `2`. What you're looking for is `++one`. – Ivan Jan 13 '20 at 10:51
  • 2
    It works if you use `return ++args;`. See https://stackoverflow.com/questions/3469885/somevariable-vs-somevariable-in-javascript – Junior Dussouillez Jan 13 '20 at 10:51
  • primitive values are handed over as value, not as reference. – Nina Scholz Jan 13 '20 at 10:52
  • 1
    Does this answer your question? [++someVariable vs. someVariable++ in JavaScript](https://stackoverflow.com/questions/3469885/somevariable-vs-somevariable-in-javascript) – James Whiteley Jan 13 '20 at 10:54
  • 1
    The are two problems in this code. The first one with post-increment ist stated in the other answer. But you have another problem with the datatype number. It's a primitive type and you are passing it to a function which in fact copies the value `one` to `arg`. `arg` is incremented after return but immediately forgotten. `one = chg(one)` would change your value in combination with `return ++arg` – southz rgw Jan 13 '20 at 11:00

4 Answers4

5

x++ is the post-increment expression, i.e. its value is x, and after that value is returned, the variable is incremented by one.

++x is the pre-increment expression, i.e. x is first incremented by one, then returned.

You'll want ++x here – or since this is a function argument, just be clearer and use x + 1; the ++ modification will have no effect anyway.

AKX
  • 152,115
  • 15
  • 115
  • 172
1

Try below code. You need to use pre-increment instead of post-increment

let one = 1;

function chg(arg) {
  return ++arg // --> shouldn't this be similar to arg + arg? but it's outputting 1
}

console.log(one);
console.log(chg(one));
ascsoftw
  • 3,466
  • 2
  • 15
  • 23
0

Just edited your code:

let one = 1;

function chg(arg) {
  return ++arg; 
}

console.log(one);
console.log(chg(one));

or can also be like this

let one = 1;

function chg(arg) {
  return arg;
}

console.log(one);
console.log(chg(++one));
Arijit Jana
  • 220
  • 1
  • 4
  • so it means, putting a ++ at the front returns the memory where i stored 1, and then it added a 1 in it's place? Also, why would it be different it i put it in a function. If let's say I instead put as below, it increments properly. `let one = 1; one++ function chg(arg) { return ++arg // --> shouldn't this be similar to arg + arg? but it's outputting 1 } console.log(one); console.log(chg(one));` – Kimimomo Jan 13 '20 at 10:53
  • Here the variable arg is not shifted by reference but only by value. – Arijit Jana Jan 13 '20 at 10:56
  • sorry tool me a while to edit that – Kimimomo Jan 13 '20 at 10:57
0
let one = 1;

function chg(arg) {
  return arg+1 // This is more readable code
}

console.log(one);
console.log(chg(one));
Wang Liang
  • 4,244
  • 6
  • 22
  • 45