Operators and Expressions
Floating point number
There will be precision problems. It is recommended not to directly use floating-point numbers to participate in the operation.
console.log(0.1+0.2);//0.30000000000000004 console.log(0.07*100);//7.000000000000001 //You cannot directly judge whether two floating-point numbers are equal let num = 0.1+0.3; console.log(num == 0.3);//false
About judging whether a number is an integer: use the remainder symbol to judge:%;
Self increasing and self decreasing operators: + +--
If pre auto increment and post auto increment are used alone, the effect is the same. If they appear in an expression, the effect is different, and so is auto decrement.
e + +: This is an expression. The expression must have a value. Only after the expression is completed will it execute + +;
Example:
let e =10;
let f = e++ + ++e;
First, execute e + +. E + + is an expression, and the expression must return a value. Therefore, after executing the expression, it is like this: 10 + + E. at this time, e + + can increase automatically after executing the expression. Therefore, e=11, so the whole expression is like this: 10 + + 11; Next, execute the following expression + + e, increase the value first and then take the value, so it is 12, so the final result is 10 + 12 = 22;
Comparison operator
< =, > =: less than or equal to, greater than or equal to
==In: js = = the default conversion data type will convert the character type to a number
18 = = '18' / / true. As long as the evaluation is equal, the data types can be different
===: all equal, 18 = = '18' / / false, value types must be equal.
Logical operator
&&: logical and, both sides are true, otherwise both are false;
Example: 3 > 5 & & 3 > 2// false;
||: logical or. If both sides are false, it will be false. Otherwise, it will be true;
Example: 3 > 5 | 3 > 2// true;
! : Logical non! true; On the contrary, it is either false or true;
Example:! 3>5; // true;
<script> let num = 9; let str = "Lifelong struggle for communism"; alert(`num>5 && str.length >= num:${num>5 && str.length >= num}`);//true alert(`num<5 && str.length >= num:${num<5 && str.length >= num}`);//false alert(`!(num<10):${!(num<10)}`);//false alert('!num:'+!num);//true alert(`!num<10:${!num<10}`);//True: I guess it should be like this:! num=false, then false < 10 = true; (false may be true when compared with a number, but false cannot be compared with a number, and the compiler will report an error); alert(!(num<10 || str.length == num))//false //There is a special case of short circuit operation in logical operators, that is, the expression on the left can determine the values of all expressions, so the operation will not be performed on the right of logical operators; console.log(123&&456);//If expression 1 is true and the result is true, expression 2 is returned console.log(0 && 456);//If expression 1 is false, expression 1 is returned console.log(123&&num++);//9 num = 10; console.log(0 && num++);//0 num = 10; alert(num); //Vice versa | logic or vice versa //If logical or expression 1 is true, return expression 1; Expression 1 is false and the result is true. Return expression 2 </script>
Assignment Operators
=: from left to right;
+= , -= , *= , /= , %= ;
Example: num + = 5; --- > num = num+5;
Comma Operator
The value of (1, 2, 3) is: 3; (take the last value from left to right)
Operator priority
Parentheses () > unary operators (+ +, --,!) > Arithmetic operator (addition, subtraction, multiplication, division and remainder) > relational operator > equality operator (=,! = =..) > Logical operators (& &, |) > assignment operators > comma operators (,)
Process control
Sequential process control
One by one
Branch process control
Select different execution statements according to different conditions;
if—else if —else..
Ternary operators: conditional expressions? Expression 1: expression 2; (if the conditional expression is true, it returns expression 1; if not, it returns expression 2)
switch:
<script> let score = prompt('Please enter your grade'); let flag = 1; while(flag !=0){ switch(parseInt(score/10)){ case 10 :alert(`${score}Divide cattle`); flag = 0;break; case 9 :alert(`${score}Sub center`); flag = 0;break; case 8 :alert(`${score}I'm serious`);flag = 0; break; case 7 :alert(`${score}The score was pretty good`); flag = 0;break; case 6 :alert(`${score}Is the score OK`); flag = 0;break; case 5 : case 4 : case 3 : case 2 : case 1 : case 0 :alert(`${score}Dispensing efforts`); flag = 0;break; default : alert('The input does not meet the format. Try again'); break; } if(flag == 1){score = prompt('Please enter your grade');12} } </script>
Circulation control process
for loop (key)
Example: for (let I = 0; I < 100; I + +) {...}
while Loop
Example: let i = 0;
while(i<100){ ...i++}
do...while Loop
Example: do{...i++} while (I < 100);
continue
End the current cycle and enter the next cycle.
break
Jump out of the current loop.