How to use the switch statement in JavaScript _ basics

Source: Internet
Author: User

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

Starting with JavaScript1.2, you can use it to handle exactly this case, using a switch statement, which is more efficient if not repeatedly using the IF ... else if statement.
Grammar

The basic syntax for a switch statement gives a expression to evaluate how several different statements are executed based on the value of the expression. The interpreter checks each of the values of the expression until a match is found. If no match occurs, the default (default) condition is used.

switch (expression)
{case
 condition 1:statement (s) break
          ;
 Case condition 2:statement (s) break
          ;
  ...
 Case condition N:statement (s) break
          ;
 Default:statement (s)
}

The interpreter indicated by the break statement is the end of a particular case. If they are omitted, the interpreter will continue to execute each statement in each of the following cases (case).

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 ("Don't so good<br/>");
      break;
 Case ' F ': document.write ("failed<br/>");
      break;
 Default:document.write ("Unknown grade<br/>")
}
document.write ("exiting switch block");
-->
</script>

This will produce the following results:

Entering switch block
good job
exiting switch block


Example:

Consider a situation where you do not use the break statement:

<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 ("Don't so good<br/>");
 Case ' F ': document.write ("failed<br/>");
 Default:document.write ("Unknown grade<br/>")
}
document.write ("exiting switch block");
-->
</script>

This will produce the following results:

Entering switch block
good job
pretty good
passed
good
Failed
Unknown Grade
Exiting switch block

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.