Home  >  Article  >  Web Front-end  >  How to use switch statement in JavaScript

How to use switch statement in JavaScript

不言
不言Original
2018-12-17 17:24:3511713browse

The switch statement is a conditional branch statement that can be used to select one of multiple code blocks to be executed. This article will introduce to you how to use the switch statement in How to use switch statement in How to use switch statement in JavaScript.

How to use switch statement in How to use switch statement in JavaScript

If the expression is more troublesome, you can use the switch statement when the following two conditions are met

Expressions and values ​​can take multiple values

When you want to change the execution based on each value

Let’s look at the syntax of the switch statement

switch (表达式or变量) {
case 值1 :
break;
case 值2 :
break;
default :
//如果表达式和变量没有对应的值,执行此步骤
break;
}

First write the expression or variable in the switch.

After that, we will write multiple values ​​and their execution code. (It can be seen by looking at the syntax that in the switch statement, the code execution is basically a parallel relationship)

Please pay attention to the description of break. case~break is a process completed.

The final default is to execute if the result of the expression does not correspond to any value. You can write it or not, depending on your needs.

Let’s look at a specific example of switch statement

Let’s write a program to define a variable as member and check the name of the person assigned to member Whether it is the value in family.

There are three people, Tom, Jerry and Holly. When their names are assigned, the browser will display "Tom is my family" and so on. When writing other people's names, show "XX is not my family".

The code is as follows

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>How to use switch statement in How to use switch statement in JavaScript</title>
</head>
<body>
<script>
var member = "Tom";
switch (member) {
case "Tom":
document.write ("Tom is my family");
break;
case "Jerry":
document.write ("Jerry is my family");
break;
case "Holly":
document.write ("Holly is my family");
break;
default:
document.write (member + "is not my family");
break;
}
</script>
</body>
</html>

The running result is as follows: the browser displays "Tom is my family".

How to use switch statement in JavaScript

When var member="Jerry";, the display result is as follows:

How to use switch statement in How to use switch statement in JavaScript

Similarly, If var member="Holly"; it will display Holly is my family.

When you enter other names, such as var member="marry"; the results are displayed as follows

How to use switch statement in How to use switch statement in JavaScript

The above is the detailed content of How to use switch statement in JavaScript. 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