Home >Web Front-end >JS Tutorial >What are the javascript selection statements?

What are the javascript selection statements?

青灯夜游
青灯夜游Original
2021-11-24 15:59:584088browse

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;".

What are the javascript selection statements?

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

1. if statement

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(测试条件) {
		测试条件真时执行
}
}

2. Switch statement

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;					
}

3. Ternary operation

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!

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