最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Postfix and prefix increments in JavaScript - Stack Overflow

programmeradmin3浏览0评论

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?

Share Improve this question edited Mar 29, 2018 at 7:43 Narendra Jadhav 10.3k15 gold badges34 silver badges44 bronze badges asked Mar 29, 2018 at 7:40 HeidelHeidel 3,25418 gold badges54 silver badges88 bronze badges 12
  • 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 Commented Mar 29, 2018 at 7:44
  • Possible duplicate of javascript i++ vs ++i – Eddie Commented Mar 29, 2018 at 7: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 Commented Mar 29, 2018 at 7:45
  • @Eddie No, it’s not about post- vs pre-increment. It’s about what a = a++; means and does. – Sebastian Simon Commented Mar 29, 2018 at 7:45
  • Don't just base it on the title alone. The answers on the post also answers this post. @Xufox – Eddie Commented Mar 29, 2018 at 7:47
 |  Show 7 more comments

9 Answers 9

Reset to default 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

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.

发布评论

评论列表(0)

  1. 暂无评论