JavaScript operators

JavaScript operator

1, Arithmetic operator

Arithmetic operators: +, -, *, /,%, respectively, are addition, subtraction, multiplication, division, modulo (remainder)

In js, the equal sign is a result of assignment, which is an assignment operation. The result on the right of the equal sign will be assigned to the left of the equal sign
Define multiple variables with one var, separated by commas
Example: if var b var a=b=a+3, the result after assignment will be returned to a first, and then assigned to B
Therefore, any operator will first return the result to the original declaration element, and then assign the value to the subsequent result
(the result will be included in the loop when it is converted into an implicit value, and then returned automatically)
To the left of the equal sign, expressions are not allowed, and operators can only be placed on the right

In decimal calculation, in order to ensure the accuracy of the value, we usually convert the corresponding power of 10 into an integer, and then use parseInt (scientific calculation method) to calculate the corresponding power of 10. The browser can accurately get what we want
Therefore, because the operation after the decimal point is not accurate, you can consider using parseint ((the power of the digits reserved for the original value x10) operator (the power of the digits reserved for the original value y*10))
When taking modulus, remember that the remainder is always less than the divisor

Addition, subtraction, multiplication and division module

var a=3;
//The equal sign is an assignment, and the result to the right of the equal sign will be assigned to the left of the equal sign
var a=3,b=4,c=5;
//Define multiple variables with one var, separated by commas
//var a,b,c;
a=b=a+3;//The result of a+3 is assigned to a first, and then the result of a+3 is assigned to b
var x=3;
console.log(x=x+3);//Any operator will return the result first and then assign a value.

Operators are not allowed on the left side of the equal sign, but on the right side of the equal sign
Due to the imprecision of the operation after the decimal point, it can be considered to use
ParseInt ((original value x * 10 reserved digit power) operator (original value * 10 reserved digit power)) / 10 reserved digit power


Non numerical operation

1. If the operator is addition, it needs special treatment. Arithmetic operators other than addition implicitly convert the contents at both ends of the symbol into numerical operation

console.log(null-true); -1

2. Non numerical addition

3. The left and right ends of a string are concatenated by an addition operator, and the other ends are converted to implicit characters

var age=20; console.log("this year"+age+"year");

4. If there is no string at both ends of the addition, it will be implicitly converted to numerical type for calculation


2, Assignment operator

a Represents an override variable   +=operator      5 is the step size
var a=5;
// a+="";// Method for quickly converting numeric value into string

In the shift operation, first convert the value into binary, then add a few zeros to shift a few bits to the left, and then convert it into decimal

4, Bitwise operator

Chinese simplified character set big five code (Taiwan traditional character) Unicode (universal code) utf-8
GBK5   GB2312      BIG5          unicode       utf-8
Bitwise operator an operator that specifically evaluates binary
//First convert the numerical value into binary, and then compare it with phase
// 1&1=1
// 1&0=0
// 0&1=0
// 0&0=0

// 1011  11
// 1010  10

// 1010  10
// 1001  9   

// 1000  8
// 0111  7

// 0000
        // After converting any numerical value into binary, find the number of 1
        // 11111
        // var s=31;
        // var n=1;
        // while(s=s&(s-1)){
        //     n++;
        // }
        // console.log(n);
// Or operation|

// 1|1=1
// 1|0=1
// 0|1=1
// 0|0=0

// The same XOR operation is 0, and the difference is 1
// 1^1=0
// 1^0=1
// 0^1=1
// 0^0=0
// key
// 5638^1234
// console.log(563810^1234); //564912
// console.log(564912^1234);//563810
// Bit non plus 1 takes negative

5, Unary operator

 var s=1;
console.log(s++);//1 returns s first, then + 1, and then assigns a new value to s
console.log(++s);//2 first + 1, then return, and then assign a value to s
 If written separately s–perhaps–s,There is no need to return the result of the operation,Regardless of use s–Still use–s,s The results are the same
s–; --s;

Little practice

var x=5;
//6 + 6+  7 +  8  +   9  +9 + 10=55
//6+  6+    7   +8   +9   +9 +10
var y=++x + x++ + x++ + x++ +x +x++ +x;//Find his value

 var x=5;
 console.log(x++*2);//10 x + + return value * 2
var x=5;

When a unary operator is encountered, distinguish the returned result from the variable value of the operation

//Little practice
 x++;//  If a unary operator is encountered, convert the variable to numeric type first

Fast conversion of string to numeric

var x="5";

6, Relational operation

< > <= >= == === != !== The result returned is a Boolean value

===Judge whether the objects on both sides of the equal sign are congruent, including value and type. NaN cannot judge, NaN =

console.log({x:1}==={x:1});//false all objects are reference relationships. The objects created by reference relationships have the same surface and different addresses

==Only judge whether the results at both ends of the equal sign are equal. If the types are different, implicitly convert them first and then compare them

console.log("3"==3);//Rotation value
console.log(1==true);//numerical value
console.log(undefined==null);//Only null and undefined are equal, others are equal

console.log(0==false); console.log(""==false); console.log(""==0);
console.log([]==[]);//false reference type
console.log([]=="");//When comparing an array with other symbols, it is first converted into a string and [] into an empty string
console.log([]==0); console.log([]==false); console.log([]==![]); //true

        // 0,false,"",NaN,null,undefined convert to Boolean false
		//var a;
            //a=>0 "" false []
        if(a===false){ //a=>false
            //a=>"" 0 NaN undefined false null
            //Non converted Boolean encountered
            //a=>null undefined


var str = NaN;
//window.isNaN implicitly converts the numeric type and then judges whether it is NaN
//Nubmer.isNaN only judges the value type without implicit conversion. If it is a non value, the default judgment result is false

isNaN Is it non numeric  true It's non numeric, false Is numeric
 Automatically and implicitly convert to numerical value, and then judge whether it is a non numerical value
// console.log(isNaN("a"));//true
// console.log(isNaN(3));//false;
// console.log(isNaN("3"));//false
// console.log(isNaN(true));//false
// console.log(isNaN(""));//false
// console.log(isNaN(" "));//false
// console.log(isNaN(NaN));//true

//Use number only if it is a numeric type IsNaN judgment
//isNaN is equivalent to a double equal sign. To judge whether it is non numeric, it is implicitly converted to numeric type
//Number.isNaN is similar to class 3. It judges whether the content is a numeric type. If it does not directly return false, only NaN returns true

7, Logical operator

		/* Logical operation */
        &&Return value does not estoppel Boolean value
       A(true) && B(true) =B(true)
       A(false) && B(true) =A(false)
       A(true) && B(false) =B(false)
       A(false) && B(false) =A(false)
	  var x=60;
         console.log(x && x+1);     //61
        var x=-10;
        console.log(x-10 && x+10);      //0
        var x;
        if((x=2) && (x=0)){

         ||  Fuse
       A(true) || B(true) =A(true)
       A(true) || B(false) =A(true)
       A(false) || B(true) =B(true)
       A(false) || B(false) =B(false)
       console.log(null || 5); console.log(null || 0);
        console.log(null || 5);        //5
        console.log(null || 0);             //0
         Fuse assignment
         var x=5;  x=x || 3;
         var x=1;
         x= --x || x++;    //  0
         x= --x || ++x;     //   1
         // When using logical and logical or, the judgment is usually converted to Boolean judgment,
         // However, the original value is not converted, and the original value is returned, not a Boolean value

		! Negation is implicitly converted to a Boolean value, and then negation

8, Ternary operator conditional operator ternary operator

Writing conditions? If the condition is 1, the result will be returned

Note: if both result 1 and result 2 are Boolean values, do not use the ternary operator and return the condition directly

The biggest disadvantage of the ternary operator is that the priority is very low (the assignment is lower, so when the assignment operator appears, the ternary operator shall be operated first)

For example: var a = 5 var B = a > 0? 3:2;

Posted by healthnut on Mon, 02 May 2022 02:54:27 +0300