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'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?
9 Answers
Reset to default 9var a = 1; a = a++;
1
is assigned toa
a++
is evaluated as1
a++
incrementsa
to2
a = {result of previous evaluation}
assigns the1
toa
so it is1
again
var b = 1; b = ++b;
1
is assigned tob
++b
incrementsb
to2
++b
is evaluated as2
b = {result of previous evaluation}
assigns the2
tob
so it is2
still
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
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.
In the case of a++
the return value is actually the original value. You’ve assigned the original value to a
again.
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;
a= a++
return the previous value of a before increment it's value.
you can just use a++
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.
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);
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.
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 Commented Mar 29, 2018 at 7:44a = a++
ora = ++a
. If you want to assign the return value of eithera++
or++a
you wouldn't want to assign them to the value you're incrementing. – Reinstate Monica Cellio Commented Mar 29, 2018 at 7:45a = a++;
means and does. – Sebastian Simon Commented Mar 29, 2018 at 7:45