Home Web Front-end JS Tutorial How to use switch statement in JavaScript

How to use switch statement in JavaScript

Dec 17, 2018 pm 05:24 PM
switch statement

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;
}
Copy after login

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>
Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Conditional control structures in PHP Conditional control structures in PHP Mar 10, 2024 pm 09:36 PM

Conditional control structure in PHP In PHP programming, conditional control structure is a very important syntax, which allows the program to execute different code blocks based on different conditions. By using conditional control structures, we can implement the branching logic of the program and determine the execution path of the program based on the result of the condition. This article will introduce the commonly used conditional control structures in PHP, including if statements, else statements, elseif statements and switch statements, and give specific code examples. The if statement is the most basic conditional control in PHP

Go language basics - switch statement Go language basics - switch statement Jul 24, 2023 pm 03:50 PM

switch is a conditional statement that is used to calculate the value of a conditional expression to determine whether the value satisfies the case statement. If it matches, the corresponding code block will be executed. Is a common way to replace complex if-else statements.

switch statement switch statement Aug 11, 2023 am 10:54 AM

The switch statement is a control structure commonly used in programming, which allows the program to execute different code blocks based on different condition values. It can replace multiple if-else statements to improve the readability and maintainability of the code. Although it has some limitations, under the right circumstances, using the Switch statement can make programs more concise and efficient.

How to use switch statement in Go language? How to use switch statement in Go language? Jun 11, 2023 am 09:11 AM

Go language is an emerging programming language. It has the characteristics of efficiency, simplicity, security and concurrency, and is very suitable for use in fields such as web development, distributed systems and cloud computing. In the Go language, the switch statement is a very commonly used language structure, which can select different branches for execution based on the value of the expression. This article will introduce the basic usage and precautions of switch statements in Go language. Basic syntax: The switch statement in Go language is different from that in other programming languages.

How to use switch statement How to use switch statement Sep 21, 2023 pm 05:48 PM

Switch statement usage: 1. The Switch statement can only be used for integer types, enumeration types and String types, and cannot be used for floating point types and Boolean types; 2. Each case statement must be followed by a break statement to prevent other cases from being executed. The code block without a break statement will continue to execute the code block of the next case; 3. Multiple values ​​can be matched in one case statement, separated by commas; 4. The default code block in the Switch statement is optional, etc. wait.

Local variable type inference in Java 10: How to use final var keyword in switch statement Local variable type inference in Java 10: How to use final var keyword in switch statement Jul 31, 2023 pm 12:31 PM

Local variable type inference in Java10: How to use finalvar keyword in switch statement As the Java language continues to evolve, each new version introduces some new features and improvements. In Java10, one of the important new features is local variable type inference. This feature allows developers to use the var keyword instead of explicit type declarations, making the code more streamlined and readable. This article will explore how to use the finalvar switch in a switch statement.

How do Java enum types work with switch statements? How do Java enum types work with switch statements? Apr 30, 2024 pm 06:48 PM

Enumeration type is a data type in Java that defines a collection of constants. With the switch statement, the following functions can be achieved: Clearly represent the value range: The enumeration type is used to define a set of immutable constant values ​​to improve code readability. Matching different enumeration constants: The switch statement allows different operations to be performed based on the enumeration constants to achieve refined control. Handling different scenarios: Through enumeration types and switch statements, various situations can be flexibly handled in actual scenarios, such as sending different email contents according to different notification types.

Golang function switch statement application skills Golang function switch statement application skills May 16, 2023 am 08:26 AM

As Golang continues to develop rapidly in recent years, it has become one of the preferred programming languages ​​​​for many developers. Among Golang's many grammatical structures, the switch statement is undoubtedly a very important part. However, many developers may only use the simplest switch statement, but do not know much about the further application skills of the switch statement. This article will introduce some common application techniques of switch statements in Golang functions, in order to help readers better understand and apply s

See all articles