In all operations, there is a sequence of operations. When I was a child, when we learned arithmetic, we all knew a rule: first multiplication and then minus, parentheses are the braces!
Similarly, in JavaScript, the operator's precedence is shown in the table below, with precedence higher than the following
Operator |
Operation |
++ |
Self-increment (note the difference before and after) |
-- |
Self-reduction (note the difference before and after) |
- |
Negation (for numbers) |
+ |
Convert to Digital |
~ |
Bitwise negation |
! |
Logical Non- |
Delete, typeof, void |
Delete the property, check the data type, return the value of undefined |
*, /, %
|
Multiply, divide, seek redundancy |
+, - |
Add, Subtract |
+ |
String connection |
<<, >>, >>> |
Left shift, signed right shift, unsigned right shift |
<, <=, >= |
Compare size, first than number, after letter |
== |
Judging if they are equal |
!= |
Judging whether it's unequal |
=== |
Determine if the identity is identical |
!== |
Determine if you are not identical |
& |
Bitwise-AND |
^ |
Bitwise XOR OR |
| |
Bitwise OR |
&& |
Logic and |
|| |
Logical OR |
?: |
Conditional operators |
= |
Assignment operators |
op= |
Operation and Assignment |
Here's a chestnut.
var a = {N:1}; var b == a = {N:2// undefined// {N:2}// A = = {N:2}// b = ={/ // x: {N:2}
// }
Analytical:
Although the assignment operator is evaluated right-to-left, there is also a very important test point: The precedence of the operator
The dot operation is better than the = assignment operation, so a.x = a = {N:2} can be understood as:
1). Declares the x attribute in the A object, while B and a simultaneously point to the object {N:1, x:undefined}, which has an unassigned X at the same time
2). Assign values from right to left
1>. Assign a value to a object--a = {N:2}, at which point the variable name a changes to the new object {N:2}
2>. For a.x (which can be understood as b.x, that is, A.x has determined the point, so it is not affected by the 1> Step) property assignment {N:2}, at this time object B, {N:1, X:{n:2}}
JavaScript basic syntax--precedence of operators