Home >Web Front-end >JS Tutorial >What are the javascript selection statements?
The selection statements include: 1. if statement, syntax "if (conditional expression) {//code}else{//code}"; 2. switch statement, syntax "switch (expression) {case Value: statement;break;..}"; 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.
This place is similar to ordinary language. The format is as follows:
if(测试条件) { 测试条件真时执行 }else { 测试条件假时执行 }
The else can be omitted. For example, the following
if(测试条件) { 测试条件真时执行 }
can be used in a single line. No need for curly braces, for example
if(3>6) alert("A"); else alert("B");
Multi-branch if statement
This is similar to other languages, multi-branch will only be executed once. The format is as follows:
if (测试条件1) { 测试条件1true执行语句 }else if(测试条件2) { 测试条件2true执行语句 }else if(测试条件) { 测试条件3true执行语句 }else { 测试条件4true执行语句 }
Nesting of if statements
if(测试条件) { if(测试条件) { 测试条件真时执行 } }
is the same as c, and the syntax format is as follows:
switch(变量) { case 值1: 等于值1执行的语句; break; case 值2: 等于值2执行的语句; break; case 值3: 等于值3执行的语句; break; case 值4: 等于值4执行的语句; break; default: 与上面的值都不相等才执行的语句; break; }
is similar to c, the format is as follows:
条件表达式?表达式1:表达式2;
[Related recommendations: javascript learning tutorial]
The above is the detailed content of What are the javascript selection statements?. For more information, please follow other related articles on the PHP Chinese website!