Operator
You need to click the picture to contact me or send me a private message or comment.
Arithmetic operator
+ - * / %
Unary operator
Unary operator: an operator with only one operand
Operator binary operator of 5 + 6 operands
++Self plus 1
– self minus 1
-
Prefix + +
var num1 = 5; ++ num1; var num2 = 6; console.log(num1 + ++ num2);
-
Post + +
var num1 = 5; num1 ++; var num2 = 6 console.log(num1 + num2 ++);
-
Guess what?
var a = 1; var b = ++a + ++a; console.log(b); var a = 1; var b = a++ + ++a; console.log(b); var a = 1; var b = a++ + a++; console.log(b); var a = 1; var b = ++a + a++; console.log(b);
summary
Pre + +: add 1 first, then participate in the operation
Post + +: participate in the operation first, and then add 1
After the above two are understood, the following two communicate with each other
Pre: subtract 1 first, and then participate in the operation
Post: first participate in the operation, then subtract 1
Sample code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Unary operator</title> </head> <body> <script> //Unary operator: only one operand is needed.-- //++: before + +: add before operation / execute after + + after operation / execute before adding //--: before --: operation after self subtraction / after execution --: operation before execution / after self subtraction // var num = 5; // // num --; // // console.log(num); // // num ++; // var num2 = 5; // // ++ num2 = 5; // // console.log(num,num2);//6,6 // console.log(num++);//5 // console.log(++num2);//6 var num1 = 5; ++ num1; var num2 = 6; console.log(num1 + ++ num2);// </script> </body> </html>
Logical operator (Boolean operator)
&&It is true at the same time as two operands, and the result is true. Otherwise, it is false. ||Or one of the two operands is true, the result is true, otherwise it is false. ! not reversed
Relational operators (comparison operators)
< > >= <= == != === !==
//==The difference between = = = and = = = is that = = only makes value comparison, if the type and value of = = are equal at the same time, then they are equal. var result = '55' == 55; // true var result = '55' === 55; // false value is equal, type is not equal var result = 55 === 55; // true
Assignment Operators
= += -= *= /= %=
For example: var num = 0; num += 5; / / equivalent to num = num + 5;
Operator precedence
Priority from top to bottom 1. () highest priority 2. Unary operator + + --! 3. Operator * /% before +- 4. Relational operator > ><= 5. Equality operator = =! = ======= 6. Logical operators First & & then|| 7. Assignment operator
Sample code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Operator</title> </head> <body> <script> /* * Operators: symbols for calculating * Arithmetic operator: + - * /%. * Arithmetic expressions: expressions connected by arithmetic operators * ①Unary operator: a symbol + + that can be operated by only one operand.-- * ②Binary operator: requires two operands var num = 10 + 5; * ③Ternary operator: three operands are required. When you write conditional judgment, you can say - > that is, ternary expression. * ④Compound operator: + = - = * = / =%.= * ⑤Relational operators: > and = = = (not strict equals) = = (strict equals)! = (not strict equals)! = = (not strict equals) * If the equation is true, return true, otherwise return false * ⑥Logical operators: * &&And - > has an expression of false, and the whole result is false. * ||Or - > an expression is true, and the whole result is true. * !The result of the expression is true. * ⑦Assignment operator:= * */ // var num = 10%3; // console.log(num); // // var num1 = 10; // num1 += 10; / / equivalent to num1 = num1 + 10 // console.log(num1) // // var num2 = 10; // num2 -= 10; / / equivalent to num2 = num1 - 10 // console.log(num2) // // var str = "5"; // var num = 5; // console.log(str == num);//true is not strict, the value is compared // console.log(str === num);//false is strict, comparing value and data type // Console.log (STR! = Num); / / false is not strict. The value is compared. // Console.log (STR! = = Num); / / true is strict, comparing value and data class // console.log(5 > 3); // console.log(5 == 3); var num1 = 10; var num2 = 20; console.log(num1 > num2 && 5 < 6);//false console.log(num1 > num2 || 5 < 6);//false var flag = true; console.log(!flag); var str = "100hh"; console.log(isNaN(str));//true isNaN() only judges the value, not the type var sum = (10 + 5) * 10;//Parentheses improve operation level console.log(sum); </script> </body> </html>