Home > Web Front-end > JS Tutorial > body text

Detailed explanation of if...else and switch statements of javaScript conditional statements

青灯夜游
Release: 2018-10-23 17:13:31
forward
2036 people have browsed it

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("出来吧,小宝贝!") 
 }
Copy after login

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("出不来了,小三不能见人,哈哈!") 
}
Copy after login

if...else can also be replaced by the ternary operator:

if(num === 1){
    num--;
}else{
    num++;
}
Copy after login

Use ternary operation symbol instead of the above statement:

(num === 1) ? num-- :num++;
Copy after login

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("玩的有点嗨,哈哈,撤了");
}
Copy after login

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、锦斌");
}
Copy after login

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!

source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template