JavaScript if ... else语句


在编写程序时,可能需要从一组给定路径中采用一个。在这种情况下,你需要使用条件语句,以使程序可以做出正确的决定并执行正确的操作。

JavaScript支持条件语句,这些条件语句用于根据不同的条件执行不同的操作。在这里我们将解释if..else语句。

if-else的流程图


以下流程图显示了if-else语句的工作方式。

1620205347353115.jpg

JavaScript支持以下形式if..else语句:

  • 如果声明

  • 如果...否则声明

  • 如果...否则,如果...声明。

如果声明


if语句是基本的控制语句,它允许JavaScript进行决策并有条件地执行语句。

语法

基本的if语句的语法如下:

if (expression) {
    Statement(s) to be executed if expression is true
}

在这里评估一个JavaScript表达式。如果结果值为true,则执行给定的语句。如果表达式为假,则不会执行任何语句。大多数时候,你将在进行决策时使用比较运算符。

请尝试以下示例,以了解如何if声明有效。

<html>
    <body>
        <script type = "text/javascript">
            <!--
                var age = 20;
         
                if( age > 18 ) {
                    document.write("<b>Qualifies for driving</b>");
                }
            //->
        </script>
        <p>Set the variable to different value and then try...</p>
    </body>
</html>
Qualifies for drivingSet the variable to different value and then try...

if...else声明


if...else语句是控制语句的另一种形式,它允许JavaScript以更受控制的方式执行语句。

语法

if (expression) {
    Statement(s) to be executed if expression is true
} else {
    Statement(s) to be executed if expression is false
}

在此评估JavaScript表达式。如果结果值为true,则执行“ if”块中的给定语句。如果表达式为假,则执行else块中的给定语句。

尝试以下代码,以了解如何在JavaScript中实现if-else语句。

<html>
    <body>
        <script type = "text/javascript">
            <!--
                var age = 15;
         
                if( age > 18 ) {
                    document.write("<b>Qualifies for driving</b>");
                } else {
                    document.write("<b>Does not qualify for driving</b>");
                }
            //->
        </script>
        <p>Set the variable to different value and then try...</p>
    </body>
</html>
Does not qualify for drivingSet the variable to different value and then try...

if...else if...语句


if...else if...语句是if…else的高级形式,使JavaScript可以根据多种条件做出正确的决定。

语法

if-else-if语句的语法如下:

if (expression 1) {
    Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
    Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
    Statement(s) to be executed if expression 3 is true
} else {
    Statement(s) to be executed if no expression is true
}

此代码没有什么特别的。这只是一系列if声明,其中每个ifelse上一条语句的子句。语句是根据真实条件执行的,如果没有一个条件为真,则else块被执行。

尝试以下代码,以了解如何在JavaScript中实现if-else-if语句。

<html>
    <body>
        <script type = "text/javascript">
            <!--
                var book = "maths";
                if( book == "history" ) {
                    document.write("<b>History Book</b>");
                } else if( book == "maths" ) {
                    document.write("<b>Maths Book</b>");
                } else if( book == "economics" ) {
                    document.write("<b>Economics Book</b>");
                } else {
                    document.write("<b>Unknown Book</b>");
                }
            //->
        </script>
        <p>Set the variable to different value and then try...</p>
    </body>
<html>
Maths BookSet the variable to different value and then try...