The previous words
Most operators in javascript are represented by punctuation marks, and a few are represented by keywords. Their syntax is concise, but their number is quite large. Operators always follow some fixed syntax. Only by understanding and mastering these contents can operators be used correctly. This article will mainly introduce the syntax overview of javascript operators
Number of operands
There are 46 operators in JavaScript. If you classify them according to the number of operands, most of them are binary operators. Their operands are two, and they combine the two expressions into complex expressions.
1 + 2;true || false;
The unary operator in javascript converts one expression into another slightly complex expression, mainly including the following 9:
++ -- - + ~ ! delete typeof voida++;typeof true;
JavaScript has only one ternary operator, which is a conditional judgment operator?:, which combines three expressions into one expression
2>1 ? 2 : 1;
Priority
Operator priority controls the order of execution of operators, and the execution of operators with high priority always precedes operators with low priority operators.
The 46 operators are divided into 14 levels of priority, from high to low:
++ -- - + ~ ! delete typeof void * / % + - << >> >>> < <= > >= instanceof in == != === !== & ^ |&&||?:= *= /= %= += -= &= ^= |= <<= >>= >>>=,
From these 14-level operator priority levels, we can see:
Unary operator > Arithmetic operator > Comparison operator > Logical operator > Tripartite operator > Assignment operator > Comma operator
[Note] The logical inversion operator belongs to the unary operator and has the highest priority
example
!2<1&&4*3+1;
The above situation is more complicated, gradually decompose the operation order
First calculate the unary operator!, !2;//false
//Then the expression becomes false < 1 && 4*3 + 1;
Calculate arithmetic operator 4*3+1;//13
//Then the expression becomes false < 1 && 13;
Calculate comparison operator <, false<1;//true
//Then the expression becomes: true && 13;//13
Parentheses can be used to forcefully specify the order of operations
2+3*5;//17(2+3)*5;//25;
Bonding
Operators have two kinds of binding properties, one is bound from left to right, with the mark L, and the other is bound from right to left, with the mark R. The binding specifies the order of operations in multiple operator expressions with the same priority.
Most operators have a combination from left to right, only unary operators, conditional operators and assignment operators have a combination from right to left.
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 priority and binding of operators determine their order of operations in complex expressions, but when subexpressions have mutual influence, the order will change.
example
a = 1;b = a++ + a-- * a++;
First analyze the expression, and according to the order of priority, the increment operator, multiplication operator, addition operator and assignment operator are respectively calculated.
First calculate the first a++;//The result is 1, a is 2
//The expression becomes b = 1 + a-- * a++;
Calculate a--;//The result is 2, a is 1
//The expression becomes b = 1 + 2 * a++;
Calculate the second a++;//The result is 1, a is 2
//The expression becomes b = 1 + 2 * 1;
So, ultimately 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 act on any data type, but they still want their operands to be data of the specified type, and most operators return a value of a specific type. In the operator rule table below, the arrow is the type of the operator operand and the arrow is the type of the operation result after the arrow is the type of the operation result.
【lvalue】
Lvalue is an ancient term that means that expressions can only appear on the left side of the operator.
In javascript, variables, object properties, and array elements are all lvalues
The operand type of increment operator++, decreasing operator-- and assignment operators is lvalue
var a = 3;a++;//33--;//Report an error({}).a += '1';//'undefined1''test' -= 'test';//Report an errorOperator rule table
Operator operation type++ Increment lval->num-- Decrement lval->num- Inverse num->num+ Convert to number num->num~ Inverse int->int! Logical non-bool->booldelete Delete attribute lval->booltypeof Detection type any->strvoid Return undefined any->undef************************************************************/% Multiply, divide, and find the residual num, num->num******************************************************************+ - Add and subtract num, num->num+ String concatenation str, str->str******************************************************<< left shift int, int->int>> signed right shift int, int->int>>> Unsigned right shift int,int->int*********************************************************************< <= > >= Compare number order num,num->bool< <= > >= Compare alphabetical order str,str->boolinstanceof Test object class obj,func->boolin Test attribute str,obj->bool***************************************************************************== Judge equal any,any->bool!= Judge unequal any,any->bool=== Judge the identity any, any->bool!== Judge non-identity any, any->bool********************************************************* & Bitwise and int, int->int************************************************************************ bitwise or int, int->int***************************************************************************************************************************************************************************|| Logic or any, any->any*********************************************************?: Conditional operator bool,any,any->any************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
The above comprehensive overview of the grammar of javascript operators is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.