2

I'm just curious about one thing. A little example in Javascript

var a = 1;
a = a++;
console.log(a); // 1

var b = 1;
b = ++b;
console.log(b); // 2

var c = 1;
c += 1;
console.log(c); //2

I understand why it works this way in case b and c, but what about a? At first the code makes an assignment a = a, the value stays the same actually, but then it should (as I see) make increment and increase value a per unit. But this not happening. Why?

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Heidel
  • 3,174
  • 16
  • 52
  • 84
  • I believe `a++` means increment a, but not assigned. Whereas `++b` is increment and assign. So at this point: `a = a++` a is incremented but assigned back to its original value. – Vijay Dev Mar 29 '18 at 07:44
  • Possible duplicate of [javascript i++ vs ++i](https://stackoverflow.com/questions/6867876/javascript-i-vs-i) – Eddie Mar 29 '18 at 07:44
  • You don't normally do `a = a++` or `a = ++a`. If you want to assign the return value of either `a++` or `++a` you wouldn't want to assign them to the value you're incrementing. – Reinstate Monica Cellio Mar 29 '18 at 07:45
  • @Eddie No, it’s not about post- vs pre-increment. It’s about what `a = a++;` means and does. – Sebastian Simon Mar 29 '18 at 07:45
  • Don't just base it on the title alone. The answers on the post also answers this post. @Xufox – Eddie Mar 29 '18 at 07:47
  • @Eddie Which one? I don’t see anything there that explains something like `a = a++;`. – Sebastian Simon Mar 29 '18 at 07:50
  • https://stackoverflow.com/a/6867888/2930661 @Xufox – Eddie Mar 29 '18 at 07:51
  • @Eddie In that one, the incremented variables are assigned to _different_ variables. This question is about assigning back to the _same_ variable. – Sebastian Simon Mar 29 '18 at 07:53
  • Big difference? Maybe you might include the answer is using variable `b` instead of `a` because those are a world of difference. Right? @Xufox – Eddie Mar 29 '18 at 07:55
  • @Eddie Yes. There is a space for huge confusion for a beginner. – Suresh Atta Mar 29 '18 at 07:57
  • Possible duplicate of [Difference between i = i++ and i = i + 1](https://stackoverflow.com/questions/42459444/difference-between-i-i-and-i-i-1). This question has actually been asked quite often in other languages like [C#](https://stackoverflow.com/q/33783989/4642212) and [Java](https://stackoverflow.com/q/7911776/4642212), but JS was a bit difficult to find. – Sebastian Simon Mar 29 '18 at 07:58
  • Sure, If you think it is not a duplicate, then it is not. That why it take 5 votes to close (if you are not gold badge). But i think it is. No need to convince me :) – Eddie Mar 29 '18 at 08:03

9 Answers9

9
var a = 1;
a = a++;
  1. 1 is assigned to a
  2. a++ is evaluated as 1
  3. a++ increments a to 2
  4. a = {result of previous evaluation} assigns the 1 to a so it is 1 again
var b = 1;
b = ++b;
  1. 1 is assigned to b
  2. ++b increments b to 2
  3. ++b is evaluated as 2
  4. b = {result of previous evaluation} assigns the 2 to b so it is 2 still
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
5

That is how post increment works

If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.

a = a++;

when a++ executes a is 2 and the expression returns 1 and a gets assigned to 1.

That is the reason you seeing the value before increment.

Note that if you don't assign back, you see the incremented value of a. You in short ovverriding the incremented value here by assigning it back.

var a = 1;
a++;
console.log(a); // 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • your example actually returns 2 instead of 1. so the answer should be adapted `var a = 1; a++; console.log(a); // 2` – Ren Dec 07 '21 at 11:53
4

It is rumored that to execute ++a, the value in memory increases and then returns. While for a++, first the value is stored in the created temporary variable, then the value of the primary variable is incremented and the value of the temporary variable is returned - thus the "expenses" for creating the temporary variable increase.

Zohid
  • 438
  • 1
  • 3
  • 14
3

In the case of a++ the return value is actually the original value. You’ve assigned the original value to a again.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Andrew
  • 7,201
  • 5
  • 25
  • 34
2

a= a++ return the previous value of a before increment it's value.

you can just use a++

Towkir
  • 3,889
  • 2
  • 22
  • 41
DJAMEL DAHMANE
  • 404
  • 4
  • 15
2

The a++ returns the value that before self-increase; While the ++a returns the value that after the self-increase;

so this is why when you call a = a++, a is equal to 1;

Hank X
  • 1,988
  • 2
  • 14
  • 32
1

The postfix operator returns the original value of a. So a is increased by the postfix ++ operator but then a is overwritten by assigning the return value of the operator, which is the value before incrementing.

CannedMoose
  • 509
  • 1
  • 10
  • 18
1

The return value of a of 1 is temporary stored for assingment, then the increment takes place, a has now the value of 2 and then the assingmet of the former stored value happens.

a = a++;

is the same like

a = (temp = a, a++, temp);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1
var a = 1;
// Here a++ means a = a; then a = a + 1

a = a++; // so you are assign first step value inside a so thats way a = 1

console.log(a); // 1

means you are storing value that time when a++ is equal to a = a. You simply assigning the value of a and replacing the value.

Pankaj Bisht
  • 986
  • 1
  • 8
  • 27