top of page

Switch Statement in Java Script

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:





79 views0 comments

Recent Posts

See All

What is CSS and its types with Examples?

CSS stands for Cascading Style Sheet. A CSS files allows us to style for HTML Content. We can present the content on our website with fonts, colors, background, borders, text formatting, links effect

Java Script program to find simple interest

<html> <head> <title> form event</title> <script language="javascript"> function si(fnm) { P=fnm.t1.value R=fnm.t2.value T=fnm.t3.value fnm.t4.value=(P*R*T)/100 } </script> </head> <body> <form> <br>

Comments


bottom of page