Switch statement In Java Script

We can use this statement if we want to execute a block of code for a situation, out of many blocks of codes for different situations. For example
 
syntax (How to write) :
 
switch(variablename)

{

case 1 :

code to be executed

break

case 2 :

code to be executed

break

case 3 :

code to be executed

break

default :

code to be executed if non of the above give cases are found

}

 
Example
 
<html>

<head>

</head>

<body>

<script type=”text/javascript”>

var day

a=parseInt(prompt(“Enter Weekday please”,””))

switch(a)

{

case 1:

document.write(“<br> It is Sunday “);

document.write(“<br> Enjoy…”);

break

case 2:

document.write(“<br> It is Monday “);

document.write(“<br> go to College “);

break

case 3:

document.write(“<br> It is Tuesday “);

document.write(“<br> go to College “);

break

case 4:

document.write(“<br> It is Wednesday “);

document.write(“<br> go to College “);

break

case 5:

document.write(“<br> It is Thrusday “);

document.write(“<br> go to College “);

break

case 6:

document.write(“<br> It is Friday “);

document.write(“<br> go to College “);

break

case 7:

document.write(“<br> It is Saturday “);

document.write(“<br> go to College “);

break

default :

document.write(“<br> Invalid weekday enter value from 1-7”)

}

 </script>

</body>

</html>

 
Understanding program :
In the above program, we have used prompt method which is used to accept input from user , we learnt this in dialog box section, parseInt function will convert the accepted value to number type because the value returned by the prompt method will be of string type and we can not compare or perform any arithmetic calculation with a string value, so we used parseInt method.
 
The output of the above program will depend on the value entered by the user, for different values different block of statements will be executed and break command will make it to jump out of switch scope.
 
Output:
I If the weekday entered by user is 4 then output will be
It is Wednesday
go to College
 
If the weekday entered by user is 1 then output will be
It is Sunday
Enjoy…