search

Home  >  Q&A  >  body text

javascript - Execution process in z=z=z++

var z = 0;
z=z=z++;
alert(z);

The pop-up window is 0, why?

and

var z = 0;
z++;z=z;
alert(z);
The difference between

?

滿天的星座滿天的星座2698 days ago979

reply all(6)I'll reply

  • 高洛峰

    高洛峰2017-07-05 10:52:36

    I’m curious whether you are doing this for doing questions or whether you have seen the author using this writing method in the code of an open source project. If it is for doing questions or written tests, I suggest you not consider such a school or company. Because this question is meaningless. , swift3 even removes the ++ operator. Life is short, and it is not worth wasting time on confusing or error-prone syntax features.

    reply
    0
  • 大家讲道理

    大家讲道理2017-07-05 10:52:36

    Two points:

    1. a = a++ is assigned first and then incremented

    2. The assignment expression has a return value, which is referred to as the value of the expression

    z=z=z++; 

    It is equivalent to assigning the value of the expression "z=z++" to z, and the value of "z=z++" is equal to "z++". "z++" first uses the current value of z and then increments it

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-05 10:52:36

    Post-increment: An expression like
    n++ will return a copy of the original value of n, and then the original value of n++
    n = n++. The post-increment operator has a higher priority than assignment
    So the right side of = will First increment n and return a copy of the original value of n
    Then perform an assignment operation to assign the original value of n to n, so the value of n remains unchanged

    reply
    0
  • 阿神

    阿神2017-07-05 10:52:36

    z++ is an expression, and the result of the expression is still z, so z=z++ is equivalent to z=z which does nothing.
    You need to understand that the logic of z++ is to return the variable first value before incrementing.

    Or you can use ++z, the pre-increment is to increment yourself first, and then return the result after the auto-increment

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:52:36

    is equivalent to

    a=z++;
    z=a;
    z=z;

    reply
    0
  • 阿神

    阿神2017-07-05 10:52:36

    ++ and = problems with the order of operations. It is recommended to take a look at the priority of operation and assignment

    reply
    0
  • Cancelreply