JavaScript switch statement is basically just an enhanced version of an "
if statement". The "if" statement usually just involve two option, the true or false values, so it’s quite limited. However if you have more variables in your equitation, you should use a switch statement. The switch statement checks the expression against the number of case options and executes a statement to match the first case. If a match is not found, it will look for another statement to execute. If no match is found, it will execute the code and the end of the switch statement. One of the
basic elements used in switch statements is the day of the week.
<! Code Sample !>
<html>
<body>
<script type="text/javascript">
var d = new Date()
theDay=d.getDay()
switch (theDay)
{
case 5:
document.write("<b>It's Friday, I'm in love!</b>")
break
case 6:
document.write("<b>Saturday: A day full of fun</b>")
break
case 0:
document.write("<b>It's family day today</b>")
break
default:
document.write("<b>Finally, the weekends!</b>")
}
</script>
</body>
</html>
<! End Sample !>
In this case, the JavaScript will produce a different message on your
web page depending on what day it is. The variable included in your statement is preceded by the word "case." A colon is used after the value. If a text is going to be used as a case, use quotation marks. If it’s a number, you don’t need to use quotes. Sunday's case is 0, Monday's is 1, Tuesday's is 2 and so on. An optional break statement is used to prevent the code to run to the next case immediately.