js conditional statement

js conditional statement
Conditional statement is to decide whether to execute or skip certain statements by judging the specified expression.
js has an if statement and a switch statement.
(1) if statement
format if (judgment condition) {output result}
Example:
if(a == 1){
   console.log(a);
}
If a=1; output a.
Generally, it will be judged that there will be many conditions, Different results will be output according to different conditions;
at this time, the else if statement will be used.
Format
if(){}
else if (){}
else{}
example
if(n == 1){
output result
}  
else if(n == 2) {
Output result
}
else{
If none of the above are executed, execute this statement
}
(2) If there are too many conditional judgments in switch
, there will be many else ifs, which will cause waste.
switch{
case 1 :
// execute code 1
break;
case 2 :
// execute code 2
break;
case 3 :
// Execute code 3
break;
}
Note: switch must have a break statement, because the case statement only says the starting point of the execution code, not the end point. So without break, the following statements will be executed in sequence until the end of the switch.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646703&siteId=291194637