Detailed description of JavaScript operators

Detailed description of JavaScript operators

An operator is a symbol that performs a series of operations.

Arithmetic operator

Arithmetic operators are used to add, subtract, multiply, divide and other operations in programs.

operatordescribe
+plus operator
-minus operator
*multiply operator
/Division operator
%Modulo operator
++Self increasing operator. (i + +: use first and then increase automatically; + + i increase first and then use)
Self subtraction operator. (i –: use first and then self subtract; – i self subtract first and then use)
        let num1=10,num2=20,num3=30,sum
        console.log(num1+num2);  // Addition operation
        console.log(num1-num2);// Subtraction operation
        console.log(num1*num2);// Multiplication operation
        console.log(num1/num2);// Division operation
        console.log(num1%num2);// Modular operation
        console.log('sum', num1++);// Self increasing operation
        console.log(++num1);// Self increasing operation
        console.log('--',num1--);// Self subtraction operation
        console.log(--num1);// Self subtraction operation

result

Comparison operator

Comparison operators are used to compare two operators

operatordescribe
<Less than operator
>Greater than operator
<=Less than or equal to operator
>=Greater than or equal to operator
==Equals operator. Judge only according to the surface value, not the data type
===Identity to operator. Judge according to the value and data type at the same time. All equal is true
!=Not equal to operator. Judge only according to the surface value, not the data type
!==Not absolutely equal to. Judge according to the value and data type at the same time. All equal is true
        let num1=10,num2=20,num3='10',sum
        console.log(num1<num2);
        console.log(num1>num2);
        console.log(num1<=num2);
        console.log(num1>=num2);
        console.log('==',num1==num3);
        console.log(num1===num3);
        console.log('!=',num1!=num3);
        console.log(num1!==num3);

result

Assignment Operators

Comparison operators are used to compare two operators

operatordescribe
=Assign the value of the expression on the right to the variable on the left
+=Add the value of the expression on the right to the variable on the left of the operator, and then assign the result to the variable on the lefta+=b, equivalent to a=a+b
-=Subtract the value of the expression on the right from the variable on the left of the operator, and then assign the result to the variable on the lefta-=b, equivalent to a=a-b
*=Multiply the variable on the left of the operator by the value of the expression on the right, and then assign the result to the variable on the lefta *=b, equivalent to a=a*b
/=Divide the variable on the left of the operator by the value of the expression on the right, and then assign the result to the variable on the lefta/=b, equivalent to a=a+b
%=The variable on the left of the operator is modeled with the value of the expression on the right, and then the result is assigned to the variable on the lefta%=b, equivalent to a=a%b
|=The variable on the left of the operator and the value of the expression on the right are logically summed, and then the result is assigned to the variable on the lefta|=b, equivalent to a=a|b
^=XOR the variable on the left of the operator with the value of the expression on the right, and then assign the result to the variable on the lefta=b, equivalent to a=ab
      let num1=30,num2=20,num3=10,sum
      console.log('sum: ',sum=num2);
      console.log('sum: ','num1:', num1,num1+=num2);
      console.log('sum: ','num1:', num1,num1-=num2);
      console.log('sum: ','num1:', num1,num1*=num2);
      console.log('sum: ','num1:', num1,num1/=num2);
      console.log('sum: ','num1:', num1,num1%=num2);
      console.log('sum: ','num1:', num1,num1&=num2);
      console.log('sum: ','num1:', num1,num1|=num2);
      console.log('sum: ','num1:', num1,num1^=num2);

result

Logical operator

Comparison operators are used to compare two operators

operatordescribeExplain in detail
&&Logic anda && b . When both a and B are true, the result is true, otherwise it is false
||Logical ora || b . When at least one of a and B is true, the result is true, otherwise it is false
!Logical non! a. When a is true, the result is false, otherwise it is true
        //   &&: the result is true only if both sides are true; Otherwise, it is false;
        //        As long as one side of the result is wrong, the return value is false

        console.log(3 > 6 && 3 > 1);    // false
        console.log(3 > 2 && 3 > 1);    // true
        console.log(6 < 5 && 5 > 4);    // false

        // ||: if the results on both sides are false, the result will be false (false); Otherwise, it is true
        //      As long as the result is correct, the return value is true

        console.log(3 > 6 || 3 > 1);    //  true
        console.log(3 > 2 || 3 > 1);    //  true
        console.log(3 > 6 || 3 < 1);    // false

        // !  :  Also known as negation, it is used to take a value opposite to a Boolean value
        console.log(!true);
        console.log(!false);

result

ternary operator

Operator? Result 1: result 2

Indicates that if the value of the operator is true, the result of the entire expression is' result 1 ', otherwise it is' result 2'.

      let time= 10, time2= 6
      console.log(time < 10 ? '0' + time : time);
      console.log(time2 < 10 ? '0' + time2 : time2);

result

Bitwise operator

Detailed explanation of the use of bit operators in JavaScript

Operator priority

prioritycombinationoperatorexplain
1towards the left.,[],()Field access, array index, function call, and expression grouping
2++, – (self subtraction), - delete,typeof,new,voidUnary operator, return data type, object creation, undefined value
3towards the left*,/,%Multiplication and division of remainder
4towards the left+,-Addition, subtraction, string concatenation
5towards the left<< ,>>,>>>Left displacement, right displacement, wordless symbol right displacement
6towards the left<,<=,>,>=,in,instanceofLess than, less than or equal to, greater than, greater than or equal to, and whether it is an instance of a specific class
7towards the left== ,!= ,=== , !==Equality, inequality, congruence, incompleteness
8towards the left&Bitwise AND“
9towards the left^Bitwise XOR“
10towards the left|Bitwise OR“
11towards the left&&Logic and
12towards the right||Logical or
13towards the right?:Conditional operator (also known as ternary operator)
14towards the right*=,/=,%=,+=,-=,<<=,>>=,>>>=,&=,^=,|=Mixed assignment operator
15towards the left,Multiple calculations

The smaller the priority number, the higher the priority

Additional explanation
delete operator

The delete operator is a unary operator.

Function: it will delete the properties of the object specified by the operand. Array elements or variables. true is returned for successful deletion and false is returned for failure.

void operator

The void operator is a unary operator.

Function: it appears before the operand. The operand can be of any type. The operand will be calculated as usual, but ignore the calculation result and return undefined.

instanceof operator

Function: instanceof is to check whether an instance is instantiated by a class or its subclasses

Tags: Javascript Front-end

Posted by kritro on Wed, 04 May 2022 13:36:20 +0300