Javascript ++ \ --, javascript --

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.