Bugs caused by JavaScript operator priority

Source: Internet
Author: User

[The following is an email sent to a colleague yesterday. To prevent leaks of trade secrets, the project name and variable name are hidden.]

========================================================== ================

Yesterday we found a bug in Nx code, which was caused by the JavaScript operator priority.

Example:

If (! A instanceof Array)

Throw new Error ("a shocould be an array ");

......

If (! B instanceof Object)

Throw new Error ("B shocould be an object ");

Cause :! Operator has a higher priority than instanceof, So calculate it first! A. The result is of the boolean type.

While

True instanceof Array;

False instanceof Array;

True instanceof Object;

False instanceof Object;

Returns false, which causes all checks to fail.

The following is the operator priority in JavaScript for your reference.

Operator priority (JavaScript)

4 (in total, 5) This article is helpful-evaluate this topic

The operator priority describes the sequence in which operations are performed during expressions. First, the operation with a higher priority is executed, and then the operation with a lower priority is executed. For example, multiply and then add.

JavaScript Operators

The following table lists the JavaScript operators in priority order. Operators with the same priority are calculated from left to right.

Operator

Description

. [] ()

Field access, array indexing, function calling, and expression grouping

++ ---~ ! Delete new typeof void

Unary operator, returned data type, object creation, undefined Value

*/%

Multiply, divide, and calculate the remainder

+-+

Addition, subtraction, String concatenation

<>>>>

Shift

<<=>> = Instanceof

Is an instance of a specific class Less Than, less than or equal to, greater than, greater than or equal to, or greater than or equal?

=! ===! =

Equal, unequal, full, incomplete, etc.

&

Bitwise "and"

^

Bitwise "XOR"

|

Bitwise "or"

&&

Logic and

|

Logic "or"

? :

Conditional operation

=OP=

Assignment and assignment operations (for example, + = and & =)

,

Multiple computing

Parentheses are used to change the computational order determined by the operator priority. This means that the expression in parentheses is calculated first, and then its value is used for the rest of the expression.

Source: http://technet.microsoft.com/zh-cn/library/z3ks45k7

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.