5.5.3 Switch multiple selection statement

Previously we discussed the if  single-selection statement and the if ..else double selection statement.   In certain situations a variable or expression is tested separately for each of value it may assume and different actions are taken for each value.  JavaScript provides the switch selection statement to handle such decision making.


execute code block 1
execute code block 2
code to be executed if n is
different from case 1 and 2



<script type = “text/ javascript”>
//You will receive a different greeting based
//on what day it is. Note that Sunday =0,
//Monday =1, Tuesday =2, etc
Var d =new Date ();
the day = d.getDay();
switch (the Day)
Document.write(“Finally Friday”);
Document.write(“Super Saturday”);
Document.write(“sleepy Sunday”);
Document.write(“I’m looking forward to this weekend!”);

In the above example the variable the Day will store the user’s choice in this case it will be the day in which you are executing the above script.  This script uses this variable to determine whether the user made a valid choice depending on the choice it will execute the specified statement, if no match is occurred the default case gets executed.

The break statement causes program control to proceed with the first statement after the switch statement.  The break statement is used because the cases in switch statement would otherwise run together.  If break is not used anywhere in a switch statement, then each time a match occurs in the statement, the statements for all the remaining cases execute.  The break statement is not required for the last case in the switch statement because program control automatically terminated there.

Sign-up for our email newsletter and get free job alerts, current affairs and GK updates regularly.
Subscribe Here (or)

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + eight =

Popup Dialog Box Powered By : XYZScripts.com
  • RSS
  • Facebook
  • Google+
  • Twitter