JS about the order of operations

Source: Internet
Author: User

First introduce the concepts involved

expression:The phrase in JS, when the interpreter encounters an expression, it calculates the phrase and then participates in the operation, and the expression is the phrase in JS.

J All S Code is composed of operators and expressions, so in addition to the operator, the other is the expression in JS.

For example 1 + 1 in this code, + is the operator, the preceding 1 and the subsequent 1 are expressions.

Classification of Expressions:

Primitive Expressions: constants (such as π), variables, direct quantities (numbers, strings, regular expressions), keywords

Initialization expressions: Initializing objects and initializing arrays

Function-Definition Expressions:

Function-Call expression:

Property-Access expression:

Object creation expression: Creating a function with the new operator

Priority level:

Priority from high to low order: property access, unary operators, multiplication, add and subtract, compare (<,>, etc.), judge Equality (==,===,! ==,! =), and, or, trinocular operations, assignment operations

Summary: The property access expression has the highest priority, followed by the operator (new also belongs to the unary operator), and the lowest priority is the assignment operation.

Example:

var a = 3; ++a = = 3;//false

The result of the above example is a numeric or Boolean type, depending on the priority of the operation, if you first judge A = = 3, and then perform the + + operation, then the result is a = 2, if the first execution of the + + operation, and then judge A = = 3, the result is false.

Knot and Sex: (Thepriority of the same time to see the combination of)

Divided into left and right combination,

JS in accordance with the right combination of: Unary operators, trinocular operations, assignment operations,

All else is followed by left-associative.

Example:

var a = 3;! a+ +;

The example above has two operators, one is! , the other is + +, these two are unary operators, if the calculation from left to right, the result is 1, but the unary operator is to follow the right combination, that is, the statement will be the first a++ operation, and then a non-operation, the result is false.

One more example:

x = a? B:c? D:e? F:g

I see the time is very ignorant, although it should not be written, but this kind of writing can exist, so we also need to know how to execute, the third-order operation is right-associative, so the above example is equivalent to: x = a? B: (c. D: (E. f:g))

And finally the Order of operations:

When an expression in JS contains an expression, the order of operations is from left to right.

var a = 1= a++ + A;

var  A = 1= a++ + ++a;

Guess what A and B are equal to each of the two examples above

JS about the order of operations

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.