What are the three types of conditional statements in JavaScript?

青灯夜游
Release: 2022-01-26 17:02:39
Original
5080 people have browsed it

Three types of conditional statements in JavaScript: 1. "if else" statement, syntax "if (condition) {...}else{...}"; 2. "switch...case" statement ;3. Ternary operation statement, syntax "Conditional expression? Expression1:Expression2;".

What are the three types of conditional statements in JavaScript?

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

Conditional judgment statements are a frequently used statement form during program development. Like most programming languages, JavaScript also has conditional judgment statements. The so-called conditional judgment refers to the program performing different operations based on different conditions, such as displaying different content based on age, and judging whether the operation is successful or failed based on a Boolean value of true or false, etc.

1. if-else statement

1. Grammar

The syntax of if-else is divided into three types:

( 1) if statement;

if(条件){ 条件为true时执行代码 }
Copy after login

(2) if else statement;

if(条件){ 条件为true时执行的代码 } else { 条件为false时执行的代码 }
Copy after login

(3) if else if else statement;

if(条件1){ 条件1为true时执行的代码 } esle if (条件2){ 条件1false条件2true } else { 都false }
Copy after login

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>IfElse</title>
</head>
<body>

<script type="text/javascript">
     var myage = 10;
    //第一种语法
    if (myage > 5){
        document.writeln("你的年龄大于5岁");
    }
    //第二种语法
    if (myage > 15){
        document.writeln("你的年龄大于15岁");
    }
    else {
        document.writeln("你的年龄小于15岁");
    }
    //第三种语法
    if (myage > 5){
        document.writeln("你的年龄大于5岁");
     }
    else if(myage < 15 ){
         document.writeln("你的年龄大于15小于5岁");
     }
    else {
        document.writeln("你的年龄大于等于15岁")
    }
</script>

</body>
</html>
Copy after login

The output result is

What are the three types of conditional statements in JavaScript?

2. switch...case statement

1, syntax

switch( 表达式 n ){
case 1 : 执行代码块 1; break;
case 2 : 执行代码块 2 ; break;
default: 与 case 1 和 case 2 不同时执行的代码
}
Copy after login

2. Working principle

First set the expression n (usually a variable). The value of the expression is then compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Please use break to prevent the code from automatically running to the next case.

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Switch</title>
</head>
<body>

<script type="text/javascript">
    var flag = 2;
    switch (flag) {
        case 1:document.write("纷纷扰扰这个世界,所有的了解。");break;
        case 2:document.write("只要让我留在你身边。");break;
        default:break;
    }
</script>

</body>
</html>
Copy after login

The output result is

What are the three types of conditional statements in JavaScript?

##Three, ternary operator

The ternary operator (also known as the conditional operator) consists of a question mark and a colon. Its syntax is as follows:

b ? x : y
Copy after login

b The operand must be a Boolean expression Formula, x and y are values ​​of any type.

  • If the return value of operand b is true, then operand x is executed and the value of the expression is returned.

  • If the return value of operand b is false, then operand y is executed and the value of the expression is returned.

Example:

Define variable a, and then detect whether a is assigned a value. If a value is assigned, use the value; otherwise set the default value.

var a = null;  //定义变量a
typeof a != "undefined" ? a = a : a = 0;  //检测变量a是否赋值,否则设置默认值
console.log(a);  //显示变量a的值,返回null
Copy after login

The conditional operator can be converted into a conditional structure:

if(typeof a != "undefined"){  //赋值
    a = a;
}else{  //没有赋值
    a = 0;
}
console.log(a);
Copy after login

can also be converted into a logical expression:

(typeof a != "undefined") && (a =a) || (a = 0);  //逻辑表达式
console.log(a);
Copy after login
In the above expression, if a has been assigned, then Execute the (a = a) expression. After execution, the (a = 0) expression following the logical OR operator will no longer be executed; if a is not assigned a value, the (a = a) expression following the logical AND operator will no longer be executed. expression, and instead executes the expression following the logical OR operator (a = 0).

Note:

In actual combat, the interference of false values ​​needs to be considered. Using typeof a != "undefined" for detection can avoid being mistaken for no value assignment when the variable is assigned false values ​​such as false, null, "", NaN, etc.

[Related recommendations:

javascript learning tutorial]

The above is the detailed content of What are the three types of conditional statements in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template