Home  >  Article  >  Web Front-end  >  Can JavaScript use if statements?

Can JavaScript use if statements?

青灯夜游
青灯夜游Original
2021-10-15 17:00:502654browse

If statements can be used in javascript. The if statement is the simplest conditional judgment statement in JavaScript. The syntax is "if (conditional expression) {//code to be executed;}"; when the conditional expression is established, that is, when the result is a Boolean value true, "{ will be executed. }" in the code.

Can JavaScript use if statements?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

if statements can be used in javascript.

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

if(条件表达式){
    // 要执行的代码;
}

When the conditional expression is established, the result is a Boolean valuetrue , the code in { } will be executed.

Example:

    <script type="text/javascript">
        var age = 20;
        if(age >= 18){      // 如果 age >= 18 的结果为 true,则执行下面 { } 中的代码
            alert("adult");
        }
    </script>

Output result:

Can JavaScript use if statements?

In javascript, in addition to the simplest if statement, there are two Upgraded form:

  • if else statement;

  • if else if else statement;

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

if(条件表达式){
    // 当表达式成立时要执行的代码
}else{
    // 当表达式不成立时要执行的代码
}

if else Multiple conditions are allowed to be defined in the if else statement Expression, and execute the corresponding code according to the result of the expression, the syntax format is as follows:

if (条件表达式 1) {
    // 条件表达式 1 为真时执行的代码
} else if (条件表达式 2) {
    // 条件表达式 2 为真时执行的代码
}
...
  else if (条件表达式N) {
    // 条件表达式 N 为真时执行的代码
} else {
    // 所有条件表达式都为假时要执行的代码
}

Tip: During the execution of if else if else statement, when it encounters a established conditional expression, it will be executed immediately The code in { } will then exit the entire if else if else statement. If there is a valid conditional expression in the subsequent code, it will not be executed.

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of Can JavaScript use if statements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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