Home Web Front-end JS Tutorial Examples of using switch expressions in js_javascript skills

Examples of using switch expressions in js_javascript skills

May 16, 2016 pm 05:22 PM
switch expression

Foreword

Switch expressions are found in many languages, such as Java, C wait, and using switch is more convenient and clearer than using if else.

The usage syntax is very simple:
Copy code The code is as follows:

switch (n)
{
case 1:
Execute code block 1
break;
case 2:
Execute code block 2
break;
default:
n Code that is not executed simultaneously with case 1 and case 2
}

The usage of various languages ​​is basically similar.

If special mention is needed, in java 1.6 and below, the variable (n) can only be an integer. The String type is supported after Java 7.

In js, the String type can be used directly.

Usage examples
Copy code The code is as follows:

<!--Add by oscar999-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Author" CONTENT="oscar999">
<script>
function funcSwitch(sFlag )
{
switch(sFlag)
{
case "Test1":
alert("Test1");
break;
case "Test2":
alert("Test2");
break;
default:;
}
}

funcSwitch("Test2");
</script>
</HEAD>

<BODY>

</BODY>
</HTML>

The logic is very simple and the code is very simple . Use string directly to distinguish.

The condition value corresponding to Case is also a variable

If the corresponding value after case is not a string, but a variable. This can be achieved in combination with RegExp.
Copy code The code is as follows:

<!--Add by oscar999-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document &lt ;/TITLE>
<META NAME="Author" CONTENT="oscar999">
<script>
var str1 = "Test1";
var str2 = "Test1";
function funcSwitch(sFlag)
{
var regExp = new RegExp(sFlag);
switch(true)
{
case regExp.test(str1):
alert( "Test1");
break;
case regExp.test(str2):
alert("Test2");
break;
default:;
}
}

funcSwitch("Test1");

</script>
</HEAD>

<BODY>

< /BODY>
</HTML>
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 Article Tags

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)

What are the differences between the Japanese and Hong Kong versions of switch? What are the differences between the Japanese and Hong Kong versions of switch? Jun 20, 2023 pm 02:06 PM

What are the differences between the Japanese and Hong Kong versions of switch?

What should I do if the switch does not respond to the TV? What should I do if the switch does not respond to the TV? Jul 03, 2023 am 11:15 AM

What should I do if the switch does not respond to the TV?

Can the switch be charged on the base all the time? Can the switch be charged on the base all the time? Jul 06, 2023 pm 04:51 PM

Can the switch be charged on the base all the time?

Can Elden's Ring be played on switch? Can Elden's Ring be played on switch? Mar 11, 2024 am 11:31 AM

Can Elden's Ring be played on switch?

What is the difference between switch lite and switch What is the difference between switch lite and switch Jun 28, 2023 pm 02:13 PM

What is the difference between switch lite and switch

Is the default option required in the switch statement? Is the default option required in the switch statement? Nov 25, 2020 pm 04:03 PM

Is the default option required in the switch statement?

Does switch32g have enough memory? Does switch32g have enough memory? Jun 20, 2023 pm 02:28 PM

Does switch32g have enough memory?

It is reported that Sega has a Nintendo Switch 2 development kit, and 'FF7: R' looks like a PS5 game when running on the Switch 2 It is reported that Sega has a Nintendo Switch 2 development kit, and 'FF7: R' looks like a PS5 game when running on the Switch 2 Sep 10, 2023 pm 05:33 PM

It is reported that Sega has a Nintendo Switch 2 development kit, and 'FF7: R' looks like a PS5 game when running on the Switch 2

See all articles