JavaScript – Conditional Statements Switch Case


What is Switch statement ?

Switch statement is used to allow the value of a variable to change the control flow of program execution.

How does Switch Case statement works ?

When the variable is equal to one of the cases, the statements for that case is executed.

How Switch Case is used ?

1. Duplicate case values are not allowed.

2. The value of the variable and the value of the case should be of same data-type.

3. The value for a case must be a constant or a literal.Variables are not allowed.

Javascript online learning !!!

Click to Learn More about – Javascript online learning

Syntax :

switch(expression){  
case value1:  
 // Piece of code to be executed;  
 break;  
case value2:  
 // Piece of code to be executed;  
 break;  
case value3:  
 // Piece of code to be executed;  
 break;
case value4:  
 // Piece of code to be executed;  
 break;
case value5:  
 // Piece of code to be executed;  
 break; 
default:   
 // Piece of code to be executed if all the values are not matched;  
}  

Break statement :

The break statement is used inside the switch to terminate a statement sequence, it is an optional statement.

Default statement :

The default statement is optional, and it must appear at the end of the switch.

1. For Integer values

Example :


<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<script>
			var a = 5;
			switch(a){
				case 1:
					document.write("The value of a = 1");
				break;
				case 2:
					document.write("The value of a = 2");
				break;
				case 3:
					document.write("The value of a = 3");
				break;
				case 4:
					document.write("The value of a = 4");
				break;
				case 5:
					document.write("The value of a = 5");
				break;
				default:
					document.write("The value of a does not match any case");

			}
		</script>
	</head>
	<body>
	</body>
</html>

Output :

The value of a = 5

2. For Char values

Example :

<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<script>
			var a = 'C';
			switch(a){
				case 'A':
					document.write("The value of a = A");
				break;
				case 'B':
					document.write("The value of a = B");
				break;
				case 'C':
					document.write("The value of a = C");
				break;
				case 'D':
					document.write("The value of a = D");
				break;
				case 'E':
					document.write("The value of a = E");
				break;
				default:
					document.write("The value of a does not match any case");

			}
		</script>
	</head>
	<body>
	</body>
</html>

Output :

The value of a = C


3. For String values

Example :

<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<script>
			var a = 'Abdul';
			switch(a){
				case 'Antony':
					document.write("The value of a = Antony");
				break;
				case 'Mark':
					document.write("The value of a = Mark");
				break;
				case 'Robert':
					document.write("The value of a = Robert");
				break;
				case 'Abdul':
					document.write("The value of a = Abdul");
				break;
				case 'Kurt':
					document.write("The value of a = Kurt");
				break;
				default:
					document.write("The value of a does not match any case");

			}
		</script>
	</head>
	<body>
	</body>
</html>

Output :

The value of a = Abdul

4. Nested Switch Case

Example :

<!DOCTYPE html>
<html>
	<head>
		<title></title>
		<script>
			var a = 'Abdul';
			var b = 'B';
			switch(a){
				case 'Antony':
					document.write("The value of a = Antony <br/>");
				break;
				case 'Mark':
					document.write("The value of a = Mark <br/>");
				break;
				case 'Robert':
					document.write("The value of a = Robert <br/>");
				break;
				case 'Abdul':
					document.write("The value of a = Abdul <br/>");
					switch(b){
						case 'A':
							document.write("The value of b = A");
						break;
						case 'B':
							document.write("The value of b = B");
						break;
						case 'C':
							document.write("The value of b = C");
						break;
						default:
							document.write("The value of b does not match any case");
					}
				break;
				case 'Kurt':
					document.write("The value of a = Kurt <br/>");
				break;
				default:
					document.write("The value of a does not match any case");

			}
		</script>
	</head>
	<body>
	</body>
</html>

Output :

The value of a = Abdul

The value of b = B