Operation statement
1, if/else if/else
As long as one of the conditions is met, the latter conditions are no longer enforced
- 1.1 A single IF
var num=10; If (num>2){alert(" you are good "); }Copy the code
- 1.2 the if/else
var num=5; If (num>0){alert(" hello ")}else{alert(" you are bad ")}Copy the code
- 1.3 the if/else if/else
var num=0; If (num > 0) {alert (" positive ")} else if (num < 0) {alet (" negative ")} else {alert (0)}Copy the code
- If/eles if else if… /else
If condition statement, if one of the conditions is satisfied, the subsequent conditions are not executed
var num=6;
if(num>0){
num--;
alert("1:"+num);
}else if(num>0 && num<10){
num++;
alert("2:"+num);
}else {
num--;
}
console.log(num)
Copy the code
2. Judgment conditions
- && means and, both conditions must be met
- | | said or, as long as the two sides of the conditions to meet one
- If it’s a single value, first convert it to a Boolean, if it’s true, the condition is true, if it’s false, the condition is not true.
Recall the only cases in which other values were converted to Booleans;
- 0
- NaN
- null
- “”
- undefined
If (true){alert(" Wash the dishes!" )} the if (false) {alert (" go brush play ")} else {alert (" go to sleep ")} the if (0) {}Copy the code
Two, in JS used to detect the data type of four ways
- typeof
- instanceof
- constructor
- Object.prototype.toString.call()
1, typeof operator details
Typeof first returns a string, the type it returns.Copy the code
- ‘number’
- ‘string’
- ‘boolean’
- ‘undefined’
- ‘object’
- ‘function’
Note: Typeof null returns 'object', which requires special memory. In addition, Typeof detects arrays, re's, and ordinary objects, which return 'object', so it cannot be subdivided using Typeof.
Typeof 12 ===> "number" typeof "zhufeng" ==== "'string' typeof false ===>' Boolean 'typeof true ====>' Boolean 'typeof Null ====>' object' typeof undefined ====>'undefined' typeof [1,2] =====>"object" typeof function(){} =>"function"Copy the code
2, BAT interview questions
2-1.
Typeof [] typeof [] 2.2 var num=parseInt("px35.5"); If (num==35.5){alert(0)}else if(num==35){alert(1)}else if(num==NaN){alert(3)}else if(typeof num=='number'){typeof num=='number'){ alert(4) }else{ alert(5) }Copy the code
Three, three operators
1, 【 grammar 】 :
Conditions? Conditional execution: a statement executed when the condition is not true
var num=5
if(num>=5){
num++
}else{
num--
}
Copy the code
Rewrite to a ternary operator
num>=5? num++:num–
2. [Special Circumstances]
Void 0 = undefined/null/void 0 = undefined/null/void 0 = undefined/null/void 0 = undefined/null/void 0
var num=5; num>=5? num++:undefinde; num>=5? num++:null; num>=5? num++:void 0;Copy the code
3, 【 multiple statements 】
If the condition is true and you want to execute more than one statement at a time, you can wrap the statement in parentheses and separate the statement with commas
var num=5; var a=3; num>=5? (num++,a--):null;Copy the code
4. 【 考题】
Change the following code to a ternary operator
var num=12; if(num>0){ if(num<10){ num++; }else{ num--; } }else{ if(num==0){ num++; num=num/10; }}Copy the code
Switch case operation statement
- Compare the value of the expression with the value of each case
- If there is a match, the correlation code is executed
Switch (expression) {case n: block break; Case n: code block break; Default: default code block}Copy the code
1. [First Acquaintance]
var num=6;
if(num==5){
num++;
}else if(num==6){
num--;
}else {
num=0;
}
console.log(num);
Copy the code
Change the above to switch case statement
var num=6;
switch (num){
case 5:
num++;
break;
case 6:
num--;
break;
default:
num=0;
}
console.log(num);
Copy the code
2. Difference between Switch Case and if Elese
If num is set to 6, use if else and switch case. Is the answer the same?
In the if else condition judgment, when two == are compared, if the data type is different, they will be converted to the same data type first and then compared. In the Switch case, the condition judgment is actually three ===, which is absolutely equal. Not only the value must be the same, but also the type must be the same, so the above different situations will occur.Copy the code
3, Use the break in the switch case
In switch case, break indicates that the condition terminates. If no break is added, the execution continues. If num= 6 or num=10, add the value of num:
var num=6;
switch (num){
case 6:
case 10:
num++;
break;
default:
num=0;
}
console.log(num);
Copy the code