JS if else statement: conditional judgment statement

Conditional judgment statement is a frequently used statement form in the program development process. Like most programming languages, JavaScript also has conditional judgment statements. The so-called conditional judgment means that the program performs different operations according to different conditions, such as displaying different content according to age, judging whether the operation is successful or failed according to the Boolean value true or false, etc.

JavaScript supports the following different forms of conditional judgment statements:

  • if statement;
  • if else statement;
  • if else if else statement;
  • switch case statement.

The following is an introduction to the use of if, if else, if else if else statements

if statement

The if statement is the simplest conditional judgment statement in JavaScript. The syntax format is as follows:

if(conditional expression) {     // code to execute; }

Sample code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JavaScript</title>
</head>
<body>
    <script type="text/javascript">
        var age = 20;
        if(age >= 18){      // 如果 age >= 18 的结果为 true,则执行下面 { } 中的代码
            alert("adult");
        }
    </script>
</body>
</html>

The running effect diagram is as follows:

 

if else statement

The if else statement is an upgraded version of the if statement. It can not only specify the code to be executed when the expression is true, but also specify the code to be executed when the expression is not true. The syntax format is as follows:

if(conditional expression){     // code to be executed when the expression is true }else{     // code to be executed when the expression is not true }



Sample code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JavaScript</title>
</head>
<body>
    <script>
       var age =prompt("请输入你的年龄:");
       if(age >=18){
        alert('你可以进入该网吧!');
       }
       else{ 
        alert('你还是未成年哦!');
       }
    </script>
</body>
</html>

Running effect diagram:

 

 

 

 

 

if else if else statement

Both if and if else statements have only one conditional expression, and the if else if else statement is their more advanced form, allowing you to define multiple conditional expressions in the if else if else statement, and execute the corresponding conditional expression according to the result of the expression The code, the syntax format is as follows:

 if (condition1) {     // code to execute if condition1 is true } else if (condition2) {     // code to execute if condition2 is true } ...   else if (condition expressionN) {     // code to be executed when conditional expression N is true } else {     // code to be executed when all conditional expressions are false }









<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        var score=prompt('请输入你的成绩:');
        if(score>=90){     
            alert('A');    //成绩90分以上输出A
        }else if(score>=80){
            alert('B');    //成绩在80-90之间输出B
        }else if(score>=70){
            alert('C');    //成绩在70-80之间输出C
        }else if(score>=60){
            alert('D');    //成绩在60-70之间输出D
        }else{
            alert('E');    //60分以下输出E
        }
    </script>
</head>
<body>
    
</body>
</html>

The running effect diagram is as follows:

 

 

Precautions

When using nested if else, if there is only one line of statement, it should also be wrapped in braces to avoid conditional ambiguity.

Guess you like

Origin blog.csdn.net/weixin_65607135/article/details/126706188