catalogue
operator
classification
-
Operators: arithmetic operator, assignment operator, comparison operator, logical operator, ternary operator
-
Expression: arithmetic expression, assignment expression, judgment expression, logical expression
Arithmetic operator
+ - * / % Surplus Self increasing++ Self subtraction--
-
The data will be converted into numbers as much as possible during operation. The operation (built-in for us, which belongs to implicit conversion) conforms to the law of Number
-
+: once a string representation is encountered, the data on the other side will also be converted to a string
-
console.log(10 + 10); console.log(10 + "10"); //"10" + "10" =>"1010" console.log(10 + 20 + "100"); //30 + "100" => "30" + "100" => "30100" console.log(true + 100); // 1 + 100 =>101 console.log(false + null); // 0 + 0 => 0 console.log("" + 100); // "" + "100" => "100" console.log(10 - "5"); // 10 - 5 = >5 console.log(10 - "5px"); // 10 - NaN =>NaN console.log(10 - null); // 10 - 0 => 10 console.log(10 - true); // 10 - 1 => 9 console.log(10 * null); // 10 * 0 => 0 console.log(10 * true); // 10 * 1 => 10 console.log(100 / ""); // 100 / 0 => Infinity // %Surplus (mold taking) console.log(100 % "5"); //0 console.log(100 % "3"); //1
-
++: whether + + is in front or + + is in the back, it means to add 1 by itself, + + is in the front, it means to add 1 by itself first, and then carry out the operation, + + is in the back, it means to carry out the operation first, and then add 1 by itself
-
--: no matter -- before or -- after, it means self subtraction of 1, -- before, it means self subtraction of 1 before operation, -- after, it means operation first and then self subtraction of 1
var a = 5; // a++; ++a; console.log(a);//6 var b = 3; // var c = ++b; // console.log(c,b); //4 4 // var c = 3; // console.log(c, b); //3 4 var e = 2; //10 // 2 + 3 + 5 + 5 + 7 - 7 - 9 + 9 console.log(e++ + e++ + ++e + e++ + ++e - e++ - ++e + e++); //15 console.log(e); //10 var f = 3; //5 // 3 + 4 + 6 - 5 - 5 + 4 - 5 - 4 + 4 + 4 console.log(f++ + f++ + ++f - --f - f-- + f++ - f-- - f-- + ++f + f++); //6 console.log(f); //5 // ++-- implicit conversion also exists var test1 = "10"; --test1; console.log(test1);//9
Assignment Operators
=Assignment assigns the value to the right of the equal sign to the left of the equal sign
+= -= *= /= %=
var a = 10; console.log(a); // Add 5 on the basis of a // Reassign a // a = 15; // console.log(a); // a = a + 5; a+=5; //Equivalent to a = a + 5; console.log(a); var b = 100; b -= 50; //<=> b = b - 50; console.log(b); //...
Comparison operator
General comparison
== be equal to > >= < <= != Not equal to
// 1. When comparing data, we will try to convert it into numbers for comparison (built-in to help us, which belongs to implicit conversion). It conforms to the law of Number console.log(10 > 5);//true console.log(10 > "5");// 10 > 5 true console.log(true > false);//1 > 0 => true console.log("100px" > 100);// NaN > 100 => false console.log(null > "");// 0 > 0 =>false console.log(null >= "");// 0 > 0 =>true console.log(10 != "20"); //10 != 20 => true // 2.null is equal to undefined, but null and undefined are not equal to other data types //null == 0 =>false console.log(null == "");//false console.log(null == undefined);//true console.log(NaN == NaN);//false console.log(NaN != NaN);//true console.log(undefined == undefined); //true console.log(undefined == 0); //false console.log(null == undefined);//true console.log(4 == 'abc'); //false console.log(NaN == NaN);//false console.log(NaN != NaN);//true console.log(null == null); //true console.log(null !== undefined);//true console.log(3 !== "3");//true console.log(null >= 0); true console.log(null <= 0); true // 3. String comparison is based on ASCII (Unicode encoded) values and is based on bits. Once a bit compares the result, it will not be compared console.log("158" > "1399999999999931231");//false // 0 - 9 :48 - 57 // A - Z: 65 - 90 // a - z: 97 - 122
Absolute comparison
===Absolutely equal to !== Absolutely not equal to
//1. Different data types must be different
//=== Absolutely equal to (not only the values should be equal, but also the data types should be the same)
// !== 10 !== "10" 10 != "10"
console.log(0 == ""); console.log(0 === ""); console.log(9 === "9"); console.log(null === undefined); console.log("10" !== 10);//true
Logical operator
&& And || Or ! wrong
-
&&And: the relationship between and indicates that all are true, talent is true, and one false is false
-
||Or: it means that the relationship between or is false only when it is false, and true is true
// &&And | or console.log(true && true); //true console.log(true && false && true); //false console.log(true || false);//true // The logical operator will automatically detect whether the current data type is true or false, which conforms to the rule of Boolean // Return the value of the corresponding position according to its own law // Short circuit operation var res = 66 && "10" && "ha-ha" && 100; console.log(res);//100 var res1 = 66 && null && "ha-ha" && 100; console.log(res1);//null var res2 = "aa" || "bb" || ""; console.log(res2); //"aa" var res3 = "" || null || undefined || NaN; console.log(res3);//NaN
-
! Non: negative
// First convert the current data to Boolean value, which conforms to the law of Number, and then reverse it console.log(!10); // true - > false console.log(!20);//true -> false console.log(!"ha-ha"); //true -> false console.log(!""); //false -> true
ternary operator
Conditions? Code segment executed when conditions are satisfied: code segment executed when conditions are not satisfied; (the ternary operator is also called question mark and colon expression, which is generally used for simple judgment, equivalent to if..else)
// Conditions? Code segment executed when conditions are satisfied: code segment executed when conditions are not satisfied; (the ternary operator is also called question mark and colon expression, which is generally used for simple judgment, equivalent to if..else) // If the condition is true, the code segment after the question mark will be executed; if the condition is false, the code segment after the colon will be executed var score = 50; score >= 60 ? console.log("Have something good for lunch") : console.log("Not at noon~~~"); // Single data will automatically detect true and false "" ? console.log("really"): console.log("false");