JavaScript-based operators and comprehensive operator precedence summaries

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators logical operators

Arithmetic operators:

plus +, minus-, multiply *, save /, seek %, + + +, minus minus- -,

The subtraction computation is exactly the same as the mathematical usage.

However, the plus + also has the function of the connection string, and other operators can also convert string numbers into numeric values, see The Implicit Conversions section in the Summary of data type conversions in JavaScript .

+ +, which isdivided into pre-and post-set, the preceding indicates that the variable is added/ minus 1before use, and the post is added / minus after use 1 .

Logical operators:

1.&&:   true true false false && can be read as "and"

2.| |: or operation, when or two contents of the operation are false , the result is false, as long as there is a content of true,  The result is true,| | can be read as "or"

3.!: non-arithmetic, negation operation, when content is true , the inverse result is false, when the content is false , the inverse result is true

Add:

&& | | Other uses of

&&

If two operands are not of type Boolean

and two values converted to Boolean are true, returning the second operand

If one of the operands is converted to a Boolean type that is False, return this number

var b=undefined&&null

Console.log (b);

back to undefined

||

If two operands are not of type Boolean

The first number is converted to a Boolean type of true, then the first number is returned without looking down

If the second number is converted to a Boolean type of true, the second value is returned .

Comparison operators:

<, >=, <=, = =,!=,===,!==

<, >=, <= and mathematics mean exactly the same thing. The following four operators are a little confused when they first touch.

= = equality operator

returns True if the two operands are equal .

! = inequality operator

returns True if the two operands are not equal .

Both operators convert the operands first (often called a forced transformation) and then compare their equality. When converting different data types, the equality and inequality operators follow the following basic rules:

1, if one operand is a Boolean value, convert it to a numeric--false before comparing equality to 0, and true to 1;

2, if one operand is a string and the other operand is a numeric value, the string is converted to a numeric value before equality is compared;
3, if one operand is an object and the other operand is not, then the object'sThe ValueOf () method is used to compare the obtained base type values according to the preceding rules;
The two operators follow the following rules when comparing them.
1,Nulland theThe undefined are equal.
2,before you can compare equality, you cannotNulland theUndefined converted to any other value.
3,if one of the operands isNaN, the equality operator returnsFalse, and the unequal operator returnsTrue Important NOTES:
even if the two operands areNaN, the equality operator also returnsFalse; Because, according to the rules,NaNNot equal toNaN.
If the two operands are objects, then the comparison is not the same object. If all two operands point to the same object,
The equality operator returnsTrue; otherwise, returnFalse

The following table lists some special cases and comparison results:

Value of an expression

Value of an expression

Null = = undefined true
"Nan" = = Nan false
5 = = NaN False
Nan = = Nan false
Nan! = Nan true
False = = 0 True

True = = 1 true
True = = 2 False
Undefined = = 0 False
Null = = 0 False
"5" ==5 true

= = = congruent operator

returns True if the two operands are equal without conversion . That is, the values and types are the same.

var result1 = ("55" = = 55); True because after conversion is equal
var result2 = ("55" = = = 55); False, because different data types are not equal

!== non-congruent operator

returns True if the two operands are not equal without conversion . Different data types are not equal.

var result1 = ("55"! = 55); False, because after conversion is equal
var result2 = ("55"!== 55); True because different data types are not equal

Assignment operators:

=,+=,-=,*=,/=

The simple assignment operator is represented by the equals sign (=), and its function is to assign the value on the right to the variable on the left.

If you add the multiplicative operator, the additive operator, or the bitwise operator before the equals sign (=), you can complete the compound assignment operation.

num = num + 10; equal to num + = 10;

The main purpose of designing these operators is to simplify assignment operations. Using them does not bring any performance gains.

Ternary operators:

? :

As an example:

var max = (Num1 > num2)? num1:num2;
in this example, Max will save a maximum value. This expression means that if NUM1 is greater than num2(The relational expression returns true), the value of NUM1 is assigned to Max,if num1 is less than or equal to num2(the relational expression returns false), the value of num2 is assigned to Max.

Bitwise operators:

JavaScript also has a bitwise operator, which needs to be converted to a 16 binary number for operation, which seems to have not been used in development yet.

operator Precedence in JavaScript

Priority level

Type of operation

Relevance of

Operator

20

Parentheses

N/A

( ... )

19

Member access

From left to right

... . ...

Member access to be calculated

From left to right

... [ ... ]

New (with parameter list)

N/A

New ... ( ... )

Function call

From left to right

... ( ... )

18

New (no parameter list)

From right to left

New ...

17

Post increment (operator behind)

N/A

... + +

Post decrement (operator behind)

N/A

... --

16

Logical Non-

From right to left

! ...

Bitwise non-

From right to left

~ ...

One-dollar addition

From right to left

+ ...

One-dollar subtraction

From right to left

- ...

Forward increment

From right to left

+ + ...

Forward Decrement

From right to left

-- ...

typeof

From right to left

TypeOf ...

void

From right to left

void ...

Delete

From right to left

Delete ...

15

Power

From right to left

... ** ...

14

Multiplication

From left to right

... * ...

Division

From left to right

... / ...

Take the mold

From left to right

... % ...

13

Addition

From left to right

..... + ...

Subtraction

From left to right

... - ...

12

Bitwise LEFT Shift

From left to right

... << ...

Bitwise RIGHT SHIFT

From left to right

... >> ...

Unsigned Right Shift

From left to right

... >>> ...

11

Less than

From left to right

... < ...

Less than or equal

From left to right

..... <= ...

Greater than

From left to right

... > ...

Greater than or equal

From left to right

..... >= ...

Inch

From left to right

.....

instanceof

From left to right

..... instanceof ...

10

Equals

From left to right

.... = = = ...

Non-equal sign

From left to right

... ! = ...

Full equals

From left to right

.... = = = = ...

Non-full equals

From left to right

... ! = = ...

9

Bitwise-AND

From left to right

... & ...

8

Bitwise XOR OR

From left to right

..... ^ ...

7

Bitwise OR

From left to right

... | ...

6

Logic and

From left to right

... && ...

5

Logical OR

From left to right

... | | ...

4

Conditional operators

From right to left

... ? ... : ...

3

Assign value

From right to left

..... = ...

... + = = ...

...-=-= ...

..... *= ...

...../= ...

.....%= ...

..... <<= ...

..... >>= ...

..... >>>= ...

..... &= ...

..... ^= ...

... | = ...

2

Yield

From right to left

Yield ...

yield*

From right to left

Yield* ...

1

Expand operator

N/A

... ...

0

Comma

From left to right

... , ...

JavaScript-based operators and comprehensive operator precedence summary

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.