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>