Comprehensive overview of javascript operator syntax

  • 2021-07-02 23:34:50
  • OfStack

Previous words

Operators in javascript are mostly represented by punctuation marks and a few by keywords. Their syntax is concise, but their number is quite large. Operators always follow some fixed syntax, only to understand and master these contents, in order to use operators correctly. This article will mainly introduce the syntax overview of javascript operator

Number of operands

javascript has a total of 46 operators, most of which are 2-ary operators (binary operator) if sorted by the number of operands, both of which have two operands and combine two expressions into complex expressions


1 + 2;
true || false;

The 1-ary operator in javascript (unary operator) converts one expression into another slightly more complex expression, consisting mainly of the following nine:


++ -- - + ~ ! delete typeof void
a++;
typeof true;

javascript has only one 3-ary operator (ternary operator), is it a conditional judgment operator? Which combines 3 expressions into 1 expression


2>1 ? 2 : 1;

Priority

Operator precedence controls the order in which operators are executed, and the operator with higher precedence is always executed before the operator with lower precedence

The 46 operators are divided into 14 levels of priority, from high to low:


++ -- - + ~ ! delete typeof void
 * / %
 + -
 << >> >>>
 < <= > >= instanceof in
 == != === !==
 &
 ^
 |
&&
||
?:
= *= /= %= += -= &= ^= |= <<= >>= >>>=
,

As can be seen from the 14-level operator priority level:

1 yuan operator > Arithmetic operator > Comparison operator > Logical operator > 3-ary operator > Assignment operator > Comma operator

[Note] Logical reverse operators belong to 1-element operators and have the highest priority

Example


!2<1&&4*3+1;

The above situation is more complicated, and the operation order is decomposed step by step

Calculate the 1 yuan operator first! ,! 2; //false


// Then the expression becomes 
false < 1 && 4*3 + 1;

Evaluate the arithmetic operator 4*3 +1; //13


// Then the expression becomes 
false < 1 && 13;

Evaluate comparison operator < , false < 1;//true


// Then the expression becomes :
true && 13;//13

You can use parentheses to force the order of operations


2+3*5;//17
(2+3)*5;//25;

Combinability

Operators have two combinations, one left-to-right, marked L, and one right-to-left, marked R. Combinability specifies the order of operations in multiple operator expressions with the same priority

Most operators have left-to-right associativity, only 1-ary operators, conditional operators, and assignment operators have right-to-left associativity


w = x + y + z;
// Equivalent to :
w = ((x + y)+ z);

++ -- - + ~ ! delete typeof void
a++;
typeof true;
0

++ -- - + ~ ! delete typeof void
a++;
typeof true;
1

The precedence and combination of operators determine the order in which they operate in complex expressions, but the order changes when sub-expressions influence each other

Example


++ -- - + ~ ! delete typeof void
a++;
typeof true;
2

Firstly, the expression is analyzed. According to the order of priority, the increment operator, multiplication operator, addition operator and assignment operator are operated respectively

Calculate the first a + + first; //Result is 1, a is 2


// The expression becomes 
b = 1 + a-- * a++;

Calculate a--; //Result is 2, a is 1


++ -- - + ~ ! delete typeof void
a++;
typeof true;
4

Calculate the second a + +; //Result is 1, a is 2


++ -- - + ~ ! delete typeof void
a++;
typeof true;
5

Therefore, the final a = 2; b = 3;


++ -- - + ~ ! delete typeof void
a++;
typeof true;
6

++ -- - + ~ ! delete typeof void
a++;
typeof true;
7

Type

Some operators can work on any data type, but still want their operands to be data of the specified type, and most operators return a value of a specific type. In the following operator rules table, the type of the operator operand is preceded by the arrow, and the type of the operation result is followed by the arrow

"Left value"

Left value (lvalue) is an old term that means that an expression can only appear to the left of an operator

In javascript, variables, object attributes, and array elements are left values

The operand types of increment operator + +, decrement operator--and assignment operator are left value


var a = 3;
a++;//3
3--;// Report an error 
({}).a += '1';//'undefined1'
'test' -= 'test';// Report an error 

Operator rule table


++ -- - + ~ ! delete typeof void
a++;
typeof true;
9

Related articles: