How to use switch statement in JavaScript
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.
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".
When var member="Jerry";, the display result is as follows:
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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.

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.

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

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.

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
