This article brings you a detailed explanation of the if...else and switch statements of javaScript conditional statements. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
if...else conditional statement
If you want a script to be executed when the condition is only true, as follows:
var num = 0;if(num ===0){ console.log("出来吧,小宝贝!") }
If you want Execute A when the condition is true, and execute B in other cases; as follows:
var num = 0;if(num ===1){ console.log("出来吧,小宝贝!") }else{ console.log("出不来了,小三不能见人,哈哈!") }
if...else can also be replaced by the ternary operator:
if(num === 1){ num--; }else{ num++; }
Use ternary operation symbol instead of the above statement:
(num === 1) ? num-- :num++;
If we have multiple scripts, we can use if...else multiple times to execute different statements according to different conditions:
var name = "Mark";if(name === "振伟"){ console.log("我是振伟哥"); }else if(name === "Mark"){ console.log("我就是小马喽"); }else if(name === "锦斌"){ console.log("我就锦斌哥"); }else if(name === "贺贺"){ console.log("我是刘贺,打死你,哈哈"); }else if(name === "老乡"){ console.log("我就是你老乡"); }else if(name ===""){ console.log("玩的有点嗨,哈哈,撤了"); }
We can also Use switch statement. If the judgment condition is the same as above, it is as follows
var name = "Mark";switch(name){ case "振伟": console.log("我是振伟哥"); break; case "Mark": console.log("我就是小马喽"); break; case "锦斌": console.log("我是振伟哥"); break; default: console.log("name is not 振伟、Mark、锦斌"); }
Note: The switch statement, case and break keywords are very important, do not underestimate them; case determines whether the current switch value is conducive to the case branch statement values are equal. break will terminate the execution of the switch statement. If there is no break, after the current case is executed, the next case will continue to be executed until a break is encountered or the switch execution ends. default, this statement will be executed when the expression does not match any previous value.
The above is the detailed content of Detailed explanation of if...else and switch statements of javaScript conditional statements. For more information, please follow other related articles on the PHP Chinese website!