Home > Web Front-end > JS Tutorial > Detailed explanation of switch usage and precautions in JavaScript

Detailed explanation of switch usage and precautions in JavaScript

伊谢尔伦
Release: 2017-07-18 14:49:43
Original
1682 people have browsed it

Syntax

The basic syntax of the switch statement gives an expression to evaluate and calculate several different statements based on the value of the expression. The interpreter checks each case for the expression's value until it finds a match. If there is no match, the default condition will be used.


switch (expression)
{
 case condition 1: statement(s)
          break;
 case condition 2: statement(s)
          break;
  ...
 case condition n: statement(s)
          break;
 default: statement(s)
}
Copy after login

The break statement instructs the interpreter to end under specific circumstances. If they are omitted, the interpreter will continue to execute each statement in each of the following cases.

We will explain the break statement in the loop control chapter.
Example:

The following example illustrates a basic while loop:


##

<script type="text/javascript">
<!--
var grade=&#39;A&#39;;
document.write("Entering switch block<br />");
switch (grade)
{
 case &#39;A&#39;: document.write("Good job<br />");
      break;
 case &#39;B&#39;: document.write("Pretty good<br />");
      break;
 case &#39;C&#39;: document.write("Passed<br />");
      break;
 case &#39;D&#39;: document.write("Not so good<br />");
      break;
 case &#39;F&#39;: document.write("Failed<br />");
      break;
 default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>
Copy after login

This will produce the following results:


Entering switch block
Good job
Exiting switch block
Copy after login

Example:

Consider such a situation, if you do not use the break statement:


<script type="text/javascript">
<!--
var grade=&#39;A&#39;;
document.write("Entering switch block<br />");
switch (grade)
{
 case &#39;A&#39;: document.write("Good job<br />");
 case &#39;B&#39;: document.write("Pretty good<br />");
 case &#39;C&#39;: document.write("Passed<br />");
 case &#39;D&#39;: document.write("Not so good<br />");
 case &#39;F&#39;: document.write("Failed<br />");
 default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>
Copy after login

This will produce the following results:


Entering switch block
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Exiting switch block
Copy after login

Javascript switch usage precautions

<script>
var t_jb51_net = 65;
switch (t_jb51_net) {
case &#39;65&#39;:
alert("字符串65。jb51.net");
break;
}
</script>
Copy after login

You will find that no dialog box pops up and alert is not executed.

Cause analysis:

What needs to be made clear here is that switch uses the congruent sign "===" when making judgments. When comparing congruent signs, the first thing to look at is the data type. It's not the same, but here, t_jb51_net is of type Number, and '65' is String.

The following code will pop up the dialog box:

<script>
var t_jb51_net = 65;
switch (t_jb51_net) {
case 65:
alert("数字65。jb51.net");
break;
}
</script>
Copy after login

The above is the detailed content of Detailed explanation of switch usage and precautions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template