Home > Article > Web Front-end > What are the three types of conditional statements in JavaScript?
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;".
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. Grammar
The syntax of if-else is divided into three types:
( 1) if statement;
if(条件){ 条件为true时执行代码 }
(2) if else statement;
if(条件){ 条件为true时执行的代码 } else { 条件为false时执行的代码 }
(3) if else if else statement;
if(条件1){ 条件1为true时执行的代码 } esle if (条件2){ 条件1false条件2true } else { 都false }
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>
The output result is
1, syntax
switch( 表达式 n ){ case 1 : 执行代码块 1; break; case 2 : 执行代码块 2 ; break; default: 与 case 1 和 case 2 不同时执行的代码 }
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>
The output result is
b ? x : yb The operand must be a Boolean expression Formula, x and y are values of any type.
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的值,返回nullThe conditional operator can be converted into a conditional structure:
if(typeof a != "undefined"){ //赋值 a = a; }else{ //没有赋值 a = 0; } console.log(a);can also be converted into a logical expression:
(typeof a != "undefined") && (a =a) || (a = 0); //逻辑表达式 console.log(a);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: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!