Js execution order of each operator

This article was originally Links: https://www.sojson.com/operation/javascript.html

https://www.jianshu.com/p/d569c6ca1060

JavaScript operator precedence

JavaScript  operator precedence, it is describing a sequential operation executed in the calculation expression of computing. Performing first operations with a higher priority, then the execution of low priority operations. For example, we often say that multiplication is performed before addition and then perform addition and subtraction.

Operators JavaScript

priority Operators Explanation Associativity
1 [].() Field access, array indexing, function calls, and expression grouping From left to right
2 ++ -- -~!delete new typeof void Unary operators, return data type, object creation, undefined
value defined
From right to left
3 *、/、% Multiplying, dividing, seeking remainder From left to right
4 +、- Addition, subtraction, string concatenation From left to right
5 <<、>>、>>> Left shift, right shift, unsigned right shift From left to right
6 <、<=、>、>=、instanceof Less than, less than, or equal to, greater than, greater than, or equal to, if
an instance of a particular class
From left to right
7 ==、!=、===、!== Equal, not equal, congruent, congruent From left to right
8 & Bitwise "and" From left to right
9 ^ Bitwise "exclusive or" From left to right
10 | Bitwise "or" From left to right
11 && And short-circuit (logical "and") From left to right
12 || Short or (logical "or") From left to right
13 ?: Conditional operator From right to left
14 =、+=、-=、*=、/=、%=、&=、|=、^=、<、<=、>、>=、>>= Mixing assignment operator From right to left
15 , Multiple computing By priority calculated, and then from right to left

Javascript parentheses processing operation described

Parentheses are used to change the calculation order determined by the operator precedence. That is, the first calculation expression in parentheses finished, then the value for the rest of the expression.

was Result = 10 * 5 + 3;
document.write(result);
document.write("<br/>");

result = 10 * (5 + 3);
document.write(result);
// output is as follows:
// 53
// 80

In the first expression, in accordance with the priority of operation, the first operation is 10 * 5, then add 3the result to 50.

In the second expression, we are open to change priorities in parentheses, let (5 + 3)the result is 8, then 8go multiplied 10, the result is80

/: Division, such as:int a = 8/9;

%: Modulo arithmetic: int a = 10%2;


Logical operators Javascript

Javascript major distinction  == and  === , empathy  != and !== relationship

There are a = '100';
There are b = 100;
document.write(a==b);
document.write(a===b);

document.write(a!=b);
document.write(a!==b);
// output is as follows:
// true
// false
// false
// true

Since Javascript is a weakly typed language, treatment is not so strict on the type of data, also resulted in the '100'==100;返回true need to compare a little tight '100'===100this time not only comparative value, but also the type of comparison, is returned false.

Similarly  != , and  !==  is the same, the comparison is the value of the former, the latter is the type and value to be compared.


Javascript Operator summary

In fact, in the course of operation, and other languages, it is best to bring direct parentheses, such as var a = 1; a = (a + 100) >>(a+1); and  var a = 1; a = a + 100>>a+1;the result is 25, the order of operations is the same, right in front of the more readable?

Guess you like

Origin www.cnblogs.com/leftJS/p/11068471.html