Previous words
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 mainly introduce JavaScript operator syntax overview
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 + + ---+ ~! Delete typeof Void2 */%3 +-4 << >> >>>5 < <= > >= instanceof in6 = = = = =!==7 &8 ^9 |10 &&11 | | : *=/=%= + = &= ^= |= <<= >>= >>>=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
The expression becomes false < 1 && 13;
Calculation comparison operator <,false<1;//true
Then the expression becomes: true && 13;//13
You can use parentheses to force a specified order of operations
2+3*5;//17 (2+3) *5;//25;
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;b = 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;b = a++ + a--* A++;console.log (A, b);//2 3
Similarly a = 1;b = 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--decrement lval->num-negation num->num+ Convert to Digital num->num~ bitwise negation int->int! Logical non-bool->booldelete delete attribute lval->booltypeof detection type any->strvoid return undefined any->undef*************************************************** \% Multiply, divide, seek remainder num,num->num******************************************************+- Add, subtract num,num->num+ string connection str,str->str********************************** << left shift int,int->int>> signed right shift int ,int->int>>> Unsigned Right Shift int,int->int*************************************************** < <= > >= Compare numeric order num,num->bool< <= > >= Compare alphabetical order str,str->boolinstanceof test Test object class Obj,func->boolin testing Properties str,obj->bool************************************* = = Judge Equal any,any->bool!= Judge unequal any,any->bool= = = Determine identity any,any->bool!== to judge non-identity any,any->bool******************** & Bitwise AND int,int->int******************************** ^ Bitwise XOR or int,int->int************************************************* *****| Bitwise OR int,int->int******************************************************&& logic and any,any->any******************************************************| | Logical OR Any,any->any******************************************************?: conditional operator bool,any,any->any*** = Assignment lval,any->any*=/=%=+=-= &am p;= 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