Switch Statement – JavaScript Basics

It is used to perform different actions on different conditions.
How does it works??
– switch expression is classified once & value of expression is compared with values of each case. The associated block of code is executed if there’s a match.

<!DOCTYPE html>
<html>
<body>
<p id="ex"></p>

<script>
var day;
switch (new Date().getDay()) {
 case 0:
 day = "Sunday";
 break;
 case 1:
 day = "Monday";
 break;
 case 2:
 day = "Tuesday";
 break;
 case 3:
 day = "Wednesday";
 break;
 case 4:
 day = "Thursday";
 break;
 case 5:
 day = "Friday";
 break;
 case 6:
 day = "Saturday";
}
document.getElementById("ex").innerHTML = "Today Is " + day;
</script>

</body>
</html>

Output :

 

Leave a Reply

avatar
  Subscribe  
Notify of