Javascript Switch Statement

The switch statement is used in JavaScript to execute one code in multiple conditions. It is same as else if statement but it is more convenient then repeated if..else..if statement. Syntax

switch (expression)  
{  
case 1: statement(s)  
break;  
case 2: statement(s)  
break;  
…..  
case n: statement(s)  
break;  
default: statements(s)  
}

NOTE: Break statement plays a dominant role in switch-case statements.

Example

<!DOCTYPE html>  
<html>  
<body>  
<script>  
var grade='B';  
var result;  
switch(grade){  
case 'A':  
result="A Grade";  
break;  
case 'B':  
result="B Grade";  
break;  
case 'C':  
result="C Grade";  
break;  
default:  
result="No Grade";  
}  
document.write(result);  
</script>  
</body>  
</html>
Try Now Output
B Grade



ADVERTISEMENT
ADVERTISEMENT