JavaScript operators (Operators)



 JavaScript has the following types of operators to allow you to do different operations:

  • Assignment Operators
  • Comparison Operators (Comparison Operators)
  • Arithmetic Operators
  • Bitwise Operators
  • Logical Operators
  • String Operators
  • Conditional (ternary) operator

From the relationship between operator and operand, JavaScript operators include so-called unary operators, binary operators and ternary operators.

A binary operator requires two operands, one before the operator and one after the operator:

operand1 operator operand2

E.g

1 + 2
x * y

A unary operator needs to have an operand, which may be before the operator or after the operator:

operator operand
// or
operand operator

E.g

x++
// or
++x

Assignment Operators

The designated operator is used to assign a value to a variable.

OperatorexampleDescription
=x = yAssign the y value to the x variable
+=x + = yThe meaning is the same as x = x + y, assign the added value of xy back to the x variable
-=x - = yThe meaning is the same as x = x-y, assign the value after subtracting xy back to the x variable
*=x * = yThe meaning is the same as x = x * y, assign the value of xy multiplied back to the x variable
/=x / = yThe meaning is the same as x = x / y, the value after dividing xy is assigned back to the x variable
%=x% = yThe meaning is the same as x = x% y, the remainder of x divided by y is assigned back to the x variable
**=x ** = yThe meaning is the same as x = x ** y, assign x to the y power value back to the x variable
<<=x <<= yThe meaning is the same as x = x << y. All the bits of x are shifted to the left by y, and the value of the bit on the right is padded with 0 and assigned back to the x variable
>>=x >>= yThe meaning is the same as x = x >> y, shift all the bits of x to the right by y bits, and the leftmost bit (sign bit) is filled with the same value as the original leftmost bit and then assigned back to the x variable
>>>=x >>>= ySame as >>=, but the leftmost bit is filled with 0
&=x &= yThe meaning is the same as x = x & y, assign the value of xy to the x variable after bitwise AND operation
^=x ^ = yThe meaning is the same as x = x ^ y, assign the value of xy to the x variable after bitwise XOR operation
|=x | = yThe meaning is the same as x = x | y, assign the value of xy to the x variable after bitwise OR operation

Comparison Operators (Comparison Operators)

It is used to compare the relationship between the two operands of the operator, and returns true or false after the comparison. Operands can be numbers, strings, expressions, or objects.

For values ​​of different types, JavaScript will try to type conversion them to the same type, and then compare them, usually to the numeric type first.

OperatorexampleDescription
==3 == var1
'3' == var1
Return true if both sides are equal
!=var1! = 4
var2! = '3'
If the two sides are not equal, return true
===3 === var1The difference with == is that === will not automatically attempt to transform, and will return true if the type and value are the same
!==var1! == '3'
3! == '3'
The difference with != is that !== will not automatically try to transform, and will return true if the type or value is different
>var2 > var1
var1 > 2
Return true if the left operand is greater than the right
>=var2 >= var1
var1 >= 3
Return true if the left operand is greater than or equal to the right
<var2 <var1
var1 <2
Return true if the left operand is less than the right
<=var2 <= var1
var1 <= 3
Returns true if the left operand is less than or equal to the right

Arithmetic Operators

Arithmetic operators are used to do common numerical operations.

OperatorexampleDescription
+var1 + 10addition
-var1 - var2Subtraction
*10 * 10multiplication
/var1 / 10division
%12% 5The modulus operator (Remainder) divides the value of an expression by the value of another expression and returns the remainder. 12% 5 equals 2
++++10
++x
x++
Increment operator (Increment), each time the value of the variable is increased by one. If the operator is before the variable, the value will be modified before the expression is executed. If the operator is after the variable, the value will be modified after the expression is executed.
Example 1: equal to 11 + + 10
Example 2: then k + j = j k value is the original value plus one
Example 3: j = k ++ j is the value of the original value of k, k, in its value will be assigned increment after j
---10
--x
x--
Decrement operator (Decrement), each time the value of the variable is reduced by one. If the operator is before the variable, the value will be modified before the expression is executed. If the operator is after the variable, the value will be modified after the expression is executed.
Example 1: --10 equal to 9
Example 2: j = --k the value of j is the original value of k minus one
Example 3: j = k-- the value of j is the original value of k, k value will Decrease after assigned to j
--x
-3
Unary negation operator (Unary negation), for example -x If x is 3 then -x is negative 3
++3
+true
Unary plus operator (Unary plus) has no meaning when used in numerical values; it is usually used for type conversion to convert operands into numerical values. For example, +true will transform the Boolean value true to the value 1

Bitwise Operators

It is used to perform binary bitwise operations. The bitwise operator treats the operand as 32 bits to do the operation, and then returns the result of the numeric type after the operation.

Operatorexamplebinary operationresultDescription
&15 & 91111 & 1001 = 10019Bitwise AND operation, if both bits are 1, the result is 1, otherwise it is 0
|15 | 91111 | 1001 = 111115Bitwise OR operation, if any bit is 1, the result is 1, otherwise it is 0
^15 ^ 91111 ^ 1001 = 01106Bitwise XOR operation, if the bits are not the same, the result is 1, otherwise it is 0
~~15~00000000...00001111 = 11111111...11110000-16Bitwise NOT operation, change all bits of 0 to 1, and 1 to 0

Bitwise shift operator: (9 in binary is 1001)

OperatorexampleDescription
<<9 << 2 = 36Left shift operation (Left shift), move all bits to the left by n positions, and add 0 to the right bits
>>9 >> 2 = 2Sign-propagating right shift, shifts all bits to the right by n positions, the leftmost bit (sign bit) is filled with the same value as the original leftmost bit, keeping the positive and negative numbers consistent
>>>19 >>> 2 = 4Zero-Fill Right Shift, the same as >>, but the leftmost bit is 0

Logical Operators

Logical operators are used to perform boolean operations, and the result of the operation returns true or false.

True and false judgment formula, everything can be converted to Boolean values, and in addition null, numerical 0NaNempty string ''and undefinedis false, the other values are true.
OperatorexampleDescription
&&expr1 && expr2Logical AND, if expr1 and expr2 are both true, it will return true, otherwise it will return false
||expr1 || expr2Logical OR, if expr1 or expr2 is true, it will return true, otherwise it will return false
!! exprLogical NOT, if expr is true, return false, otherwise return true

But &&and ||there are more special place, if the calculated value is not a boolean yuan, in fact, returns the value of one operand, see example below.

// foo is Dog
var foo = 'Cat' && 'Dog';

// foo is false
// Because && encounters a false operand, it will return directly and will not continue to judge (Short-circuit evaluation)
var foo = false && 'Cat';

// foo is Cat
// Because || encounters a true operand, it will return directly, and will not continue to judge (Short-circuit evaluation)
var foo = 'Cat' || 'Dog';

// foo is Cat
var foo = false || 'Cat';

Conditional (ternary) operator

condition ? val1 : val2

If condition is true, return the result of val1, otherwise return the result of val2. E.g:

// If the age variable is greater than or equal to 18, the status will be adult
// Conversely, if the age variable is less than 18, the status will be minor
var status = (age >= 18) ? 'adult' : 'minor';

Operator precedence

Various operators in dealing with priorities (precedence) is in accordance with the general rules of arithmetic, multiplication and division after the first addition and subtraction, if you have some arithmetic to give priority treatment, can be placed in ()small brackets, for example:

100 + 50 / 2 = 125
(100 + 50) / 2 = 75

Here is sorted out the priority of various operators of JavaScript, from high to low:

Operatorpriority
member. []
call / create instance() new
negation/increment! ~ - + ++ -- typeof void delete
multiply/divide* /%
addition/subtraction+-
bitwise shift<< >> >>>
relational< <= > >= in instanceof
equality== != === !==
bitwise-and&
bitwise-xor^
bitwise-or|
logical-and&&
logical-or||
conditional?:
assignment= += -= *= /= %= <<= >>= >>>= &= ^=
paragraph,

Post a Comment

0 Comments