Javascript Switch Case

Javascript Switch Statement tests the value of a given variable or expression against a no. of statements and when a match is found, a block of statement associated with that case is executed. or we can say that it is a alternative of if-else statement.

Example of Javascript Switch Case

<html>
<head>
<style>
body
{
padding:0px;
margin:0px;
}
h2
{
margin-left:150px;
width:400px;
background-color:orange;
font-family:comic sans ms;
font-size:20px;
color:white;
text-align:center;
}
#sss
{
margin-left:280px;
border-radius: 15px;
background-color:black;
color:white;
}
</style>
<title>we are using switch statement in this javascript Example</title>
</head>
<body>
<h2>we are using switch statement</h2>
<input id=”sss” type=”button” value=”Use switch” onClick=”s1();”>
<script>
function s1(){
var yourgrade=”A”;
switch(yourgrade)
{
case “A”: prompt(“good job”);
break;
case “B”: alert(“Preety good”);
break;
case “c”: document.write(“Fail”);
}
}
</script>
</body>
</html>

Result of  Javascript Switch Case :-

 

Javascript Switch Case




Follow Us

Contact Us

tutorialworldin@yahoo.com

Back to Top