Switch Statement:
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case and the variable being switched on is checked for each switch case.Instead of using the if-else-if ladder, the switch statement is available itsn Java script.
Syntax:
switch(expression)
{
case 1:
statement:
break;
case 2:
statement:
break;
case 3:
statement;
break;
..................
...................
.................
case n:
statement;
break;
default:
statement;
Program:
<!doctype HTML>
<HTML>
<head>
<title>Switch Statement</title>
</head>
<body>
<script>
var number, day
number=prompt("Enter number 1 to 7 ")
switch(number)
{
case "1":day="Sunday";
break;
case "2":day="Monday";
break;
case "3":day="Tuesday";
break;
case "4":day="Wednesday";
break;
case "5":day="Thursday";
break;
case "6":day="Friday";
break;
case "7":day="Saturday";
break;
}
document.write("You entered number "+number+"<br>");
document.write("day is " +day);
</script>
</body>
</html>
Output:
Comments