Thumb

JavaScript Switch Statement

1/22/2020 7:01:09 AM

In the JavaScript have two conditional statement one is if else and another is switch statement. Now switch statement is many times use to the check the statement or check the condition. When we use switch statement then we use case. When we use switch then each value called case. Then every switch value matches the case when match the case then this block of code executes otherwise default block execute. Now given bellow the switch case code and explain the code:

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<script type="text/javascript">
var day;
switch (3) {
  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";
}
console.log(day);
</script>
</body>
</html>

In the Switch Statement we use the day variable when the match the case by the switch parameter value then the match case value stores the day variable. After store value print the console using “console.log”.