xTable of Contents [1] Number of [2] precedence [3] binding [4] type [5] rules in front of the statement
Most of the operators in JavaScript are represented by punctuation, a few are represented by keywords, their syntax is concise, and their numbers are quite large. Operators always follow some fixed syntax and only understand and master them in order to use operators correctly. This article will focus on the syntax overview of JavaScript operators
Number of operations
There are 46 operators in JavaScript, and if they are categorized according to the number of their operands, most of them are two-dollar operators (binary operator) with two operands, which combine two expressions into complex expressions
1 + 2; true false;
The unary operator (unary operator) in JavaScript converts an expression to another slightly more complex expression, consisting mainly of the following 9:
Delete typeof void
a+ +; typeof true;
JavaScript has only one ternary operator (ternary operator), which is a conditional-judgment operator?: it merges three expressions into one expression
2>1? 2:1;
Priority level
Operator precedence controls the order in which operators are executed, and the execution of operators with high precedence is always preceded by operators with lower precedence operators
The 46 operators have a total of 14 priority levels, from high to Low:
1 Deletetypeofvoid2 */%3 +-4 << >> >>>5 instanceof in 6 = = = = = =!==7 &8 ^9 |10 &&11 | | 12? :*=/=%= + = = &= ^= |= <<= >>= >>>=14,
This 14-level operator priority level shows:
Unary operators > Arithmetic operators > Comparison operators > Logical operators > Ternary operators > Assignment operators > Comma operators
[note] The logical inverse operator is a unary operator with the highest precedence
Example
!2<1&&4*3+1;
Like the above, the situation is more complex, and gradually decompose its order of operations.
Computes the unary operator first!,!2;//false
// then the expression becomes false < 1 && 4*3 + 1;
Calculating arithmetic operators 4*3+1;//13
// then the expression becomes false < 1 && 13;
Calculation comparison operator <,false<1;//true
// The expression then becomes: true && 13; // -
You can use parentheses to force a specified order of operations
2+3*5; // - (2+3); // ;
Binding nature
Operators have two kinds of binding, one is from left to right, the notation is L, one is from right to left, the mark is R. binding specifies the order of operations in multiple operator expressions with the same precedence
Most operators have a left-to-right binding, and only the unary, conditional, and assignment operators have right-to-left binding
w = x + y + Z; // equivalent to:W = ((x + y) + Z);
w = x = y = Z; // equivalent to:w = (x = (y = z));
Q = a? B:c? D:e? f:g; // equivalent to:q = a? B: (c. D: (E. f:g));
The precedence and binding of operators determines their order of operations in complex expressions, but the order changes when sub-expressions affect each other.
Example
A = 1= a++ + a--* a++;
In this expression, the increment operator, the multiplication operator, the addition operator, and the assignment operator are respectively evaluated according to the order of precedence.
Calculate first a++;//result to 1,a to 2
// The expression becomes b = 1 + a--* a++;
Calculated a--;//results of 2,a to 1
// The expression becomes b = 1 + 2 * a++;
Calculates a second a++;//result of 1,a of 2
// The expression becomes b = 1 + 2 * 1;
So, finally a = 2; b = 3;
A = 1= a++ + a--* a++; Console.log (A, b); // 2 3
// similarly a = 1= a--* a++ + a++; Console.log (A, b); // 2,1
Type
Some operators can work on any data type, but still want their operands to be of the specified type of data, and most operators return a value of a specific type, in the following operator rule table, before the arrow is the type of the operator operand, and the type of the result of the operation after the arrow
"Left value"
An lvalue (Lvalue) is an ancient term that indicates that an expression can only appear to the left of the operator
In JavaScript, variables, object properties, and array elements are both lvalue
Increment operator + +, decrement operator--and the operand type of the assignment operator are Lvalue
var a = 3; a+ +; // 33--; // Error ({}). A + = ' 1 '; // ' undefined1' test '-= ' test '; // Error
Operator Rule table
operator operation type
+ + Incremental Lval->Num--Reducing the amount of lval->Num-Seeking anti-num->Num+ Convert to Digital num->Num~ Bitwise negationint-int! Logical non-bool->BOOLDeleteDelete attribute lval->BOOLtypeofDetection Type any->StrvoidBack to undefined any->undef\% Multiply, divide, seek remainder num,num->Num+-Add, subtract num,num->Num+ String Connection str,str->Str<< left Shiftint,int-int>> signed Right Shiftint,int-int>>> Unsigned Right Shiftint,int-int< <= > >= Compare Digital Order num,num->BOOL< <= > >= compare alphabet Order str,str->BOOLinstanceofTest object class Obj,func->BOOLinchTest Properties Str,obj->BOOL= = Judge Equal any,any->BOOL! = Judge Unequal any,any->BOOL= = = Determine identity any,any->BOOL!== Judging non-identical any,any->BOOL& Bitwise ANDint,int-int^ Bitwise XOR ORint,int-int******************************************************| Bitwise ORint,int-int&& Logic and Any,any-> any******************************************************|| Logical OR Any,any-> any?: Conditional operator Bool,any,any-> any= assigned value lval,any-> any*=/=%=+=-= &= operation and Assignment lval,any-> any^= |= <<=>>= >>>=******************************************************, ignoring the first operand, Any,any-Any returns a second operand
Resources
"1" Ruan one peak JavaScript standard reference Tutorial--operator http://javascript.ruanyifeng.com/grammar/operator.html#toc29
"2" JavaScript authoritative Guide (6th edition), chapter 4th expressions and operators
JavaScript operator Syntax overview