Home > Web Front-end > Front-end Q&A > Can JavaScript use if statements?

Can JavaScript use if statements?

青灯夜游
Release: 2021-10-15 17:00:50
Original
2723 people have browsed it

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(条件表达式){
    // 要执行的代码;
}
Copy after login

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>
Copy after login

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{
    // 当表达式不成立时要执行的代码
}
Copy after login

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 {
    // 所有条件表达式都为假时要执行的代码
}
Copy after login
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!

Related labels:
source:php.cn
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template