Java Script


JavaScript Tutorial


Admission Enquiry Form


What is Switch Statement in JavaScript?

Switch statement is used to select one choice out of many in JavaScript.It is just like if...else if statement. But it is better than if..else..if because it can be used with numbers, characters etc..

Syntax of Switch Statement...

   switch(expression)
   {
   case value1: 
    //statements to be executed
    break; 
    case value2:
    //statements to be executed
    break;
    .........
    default:
    //statements to be executed
    }

Example of Switch Statement for numbers Using prompt() function:

<html>
<head>
<title>switch statement</title>
</head>
<body>
<script>
var num;
num=parseInt(prompt("Enter a number from 1-7 for days of the week!"));
switch(num)
{
case 1:
document.write("Day is Sunday");
break;
case 2:
document.write("Day is Monday");
break;
case 3:
document.write("Day is Tuesday");
break;
case 4:
document.write("Day is Wednesday");
break;
case 5:
document.write("Day is Thursday");
break;
case 6:
document.write("Day is Friday");
break;
case 7:
document.write("Day is Saturday");
break;
default:
document.write("Wrong Input.Try Again!");
}
</script>
</body>
</html>

Output

Enter a number from 1-7 for days of the week! 3
Day is Tuesday


In the above example, if the user enters '1',then 'case 1' will be executed. If the user will enters '2' then 'case 2' will be executed and so on .......up to case '7'.According to our output , user enters '3' and 'case 3' get executed and give the above output.Suppose, if the user will enter any value except 1 to 7 then default will be executed.


Example of Switch Statement for characters Using prompt() function:

<html>
<head>
<title>switch statement</title>
</head>
<body>
<script>
var grade;
grade=prompt("Enter a Grade in Capital Letter");
switch(grade)
{
case 'A':
document.write("Grade A");
break;
case 'B':
document.write("Grade B");
break;
case 'C':
document.write("Grade C");
break;
default:
document.write("No Grade");
}
</script>
</body>
</html>

Output

Enter a Grade in Capital Letter
Grade A


In the above example, if the user enters 'A' and above output is generated i.e. 'Grade A'.

In switch statement , if we will not use break statement then all the cases will be evaluated.


Example of Switch Statement without using break:

<html>
<head>
<title>switch statement</title>
</head>
<body>
<script>
var grade;
grade=prompt("Enter a Grade in Capital Letter");
switch(grade)
{
case 'A':
document.write("Grade A");
case 'B':
document.write("Grade B");
case 'C':
document.write("Grade C");
default:
document.write("No Grade");
}
</script>
</body>
</html>

Output

Enter a Grade in Capital Letter
Grade BGrade CNo Grade


In the above example, we did not use break statement.Here ,the user enters 'B' and it stores into grade variable.Then it is passed to switch case.Then the control checks the 'case A'.That is not matched to user input and it is switches to 'case B' and it is matched here.It executes the 'case B' but also the other cases because we did not use here break statement.So, you can see the above output.