Javascript ++ \ --, javascript --
Increment (++) The increment operator increments (adds one to) its operand returns a value.
- If used postfix, with operator after operand (for example, x ++), then it returns the value before incrementing.
- If used prefix with operator before operand (for example, ++ x), then it returns the value after incrementing.
The increase (++) ++ operator adds 1 to its operands and returns a value.
- If it is in the form of a suffix and the operator is behind the operand (for example, x ++), the return value is x before the increase.
- If the prefix is used and the operator is before the operand (for example, ++ x), the return value is the increasing x.
The ++ operator does two things: 1. Add 1 to the operand (in any case) 2. Return a value. The returned value is determined by the operator's position.
Instance: It appears in the code
X ++; // as a separate row of statements
Or
++ X; // used as a separate row of statements
The results are the same, because the returned values do not receive objects, but all are added with 1, and the value of x is added with 1.
Only when they are part of an expression can the operator's position be affected. For example, suffix format:
Var x = 3; var y = x ++; // The value of y is 3, and the value of x is 4.
The priority of the ++ operator is higher than that of the = value assignment operator. Therefore, first perform the x ++ operation. After adding 1 to x, it becomes 4. ++ is a suffix, the returned value is 3 before x auto-increment. Therefore, the expression is equivalent:
Var y = 3; // The value of y is 3.
Prefix format:
Var a = 3; var B = ++ a; // The value of B is 4, and the value of a is 4.
The priority of the ++ operator is higher than that of the = value assignment operator. Therefore, the first operation of ++ a changes from 1 to 4. ++ is a prefix, the returned value is the auto-increment value of a, which is 4. Therefore, the expression is equivalent:
Var B = 4; // The value of B is 4.
-- The same operator.
Source: mozilla-increment