JavaScript Switch Getting Started walkthrough tutorial

Source: Internet
Author: User

JavaScript Switch Getting Started walkthrough tutorial

Conditional declarations in JavaScript are used to perform different actions according to different conditions.


Example
Switch Statement
How to compose a switch statement.
<body>
<script type= "Text/javascript" >
var d = new Date ();
Theday=d.getday ();
Switch (theday)
{
Case 5:
document.write ("<b>finally friday</b>");
Break
Case 6:
document.write ("<b>super saturday</b>");
Break
Case 0:
document.write ("<b>sleepy sunday</b>");
Break
Default
document.write ("<b>i ' m really looking forward to this weekend!</b>");
}
</script>
<p>this JavaScript would generate a different greeting based on what day it is. Note that sunday=0, Monday=1, tuesday=2, etc.</p>
</body>

JavaScript switch statement for
You should use the switch statement if you want to select a lot of blocks of code that will be executed.

Grammar

Switch (n)
{
Case 1:
Execute code block 1
Break
Case 2:
Execute code block 2
Break
Default
Code to was executed if N is
Different

This is the principle of it: first, we have a single expression (the most common variable), which is evaluated once. The expression of value, then with values for each case in the structure. If there is a matching block of code with the execution of the case. Take advantage of the rest to prevent code from running to the next case automatically.

For example

<script type= "Text/javascript"
//you would receive a different greeting based
//on what day it is. Note that sunday=0,
//monday=1, tuesday=2, etc.
var d=new Date ();
Theday=d.getday ();
Switch (theday)
{
Case 5:
  document.write ("Finally Friday"),
  Break,
Case 6:
  document.write ("Super S Aturday ");
  break;
Case 0:
  document.write ("Sleepy Sunday");
  break;
Default:
  document.write ("I ' m lo OKing forward to this weekend! ");
}
</script>

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.