Javascript Switch

If there is condition to compare the value with many different cases and execute the statement within the matched case. Switch statement executes line by line and read all the cases. If the case value is matched with a value passed in the switch then statement within the match case is executed. We can write break to terminate the execution of the further statement when the match case is found.

Syntax

switch(expression) {
 case value1
    Statement 1;
    break;
  case value2
    Statement 2;
    break;
  Default
    Statement 3;
} 

Example

In this given example, we have assigned a month variable with month number '5' and passed to the switch statement. Within switch statement, it pass through different cases and execute only the statement with matched case, i.e 'Summer Holiday'.

<script>
var month = 5;
switch(month){
case 1:
   document.write('Winter Holiday');
   break;
case 5:
	document.write('Summer Holiday');
	break;
default:
	document.write('Working Month');
}
</script>

Output





Read more articles


General Knowledge



Learn Popular Language