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:
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
<!--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.
<!--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>
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>
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 < ;/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
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

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

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?

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

Can Elden's Ring be played on switch?

What is the difference between switch lite and switch

Is the default option required in the switch statement?

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
