search

Home  >  Q&A  >  body text

javascript - Questions about i++

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

This is understandable.

But this:

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

Why is 1 still output here? Even if i = 1 is assigned first, the operation of i should still be executed, printing i is still 1.

我想大声告诉你我想大声告诉你2814 days ago637

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:45:31

    i = i++Principle:

    • Take out the value of variable i and put it in a temporary variable.

    • Increase the value of variable i.

    • Use the value of the temporary variable as the value of i before the auto-increment operation.

    After the above three steps, although the variable i was incremented in the second step, the original value was assigned to it after the third step, so the final output result is 1.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:45:31

    http://stackoverflow.com/ques... Refer to this, although it is java

    i++虽然i加1了,但因为后置++,所以执行i=i(此时i指原来的值1),所以就等于是i=1了。相当于i++It’s useless

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:45:31

    i++ return value is 1

    reply
    0
  • Cancelreply