Home > Web Front-end > JS Tutorial > Detailed explanation of the usage of switch statement in JavaScript_Basic knowledge

Detailed explanation of the usage of switch statement in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 15:56:50
Original
1658 people have browsed it

You can use multiple if...else if statements, as in the previous chapter, to execute multiple branches. However, this is not always the best solution, especially when all branches depend on the value of a single variable.

Starting with JavaScript 1.2, you can use it to handle exactly this situation, using a switch statement, which does so more efficiently if instead of repeatedly using if...else if statements.
Grammar

The basic syntax of the

switch statement gives an expression to evaluate and calculate several different statements based on the value of that expression. The interpreter checks each case for the expression's value until a match is found. 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 terminate 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='A';
document.write("Entering switch block<br />");
switch (grade)
{
 case 'A': document.write("Good job<br />");
      break;
 case 'B': document.write("Pretty good<br />");
      break;
 case 'C': document.write("Passed<br />");
      break;
 case 'D': document.write("Not so good<br />");
      break;
 case 'F': 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 the break statement is not used:

<script type="text/javascript">
<!--
var grade='A';
document.write("Entering switch block<br />");
switch (grade)
{
 case 'A': document.write("Good job<br />");
 case 'B': document.write("Pretty good<br />");
 case 'C': document.write("Passed<br />");
 case 'D': document.write("Not so good<br />");
 case 'F': 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

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