JS Switch Case Statement
JavaScript Switch Case Statement
JavaScript Switch Case Statement
- Switch Case statement is used when we have to chose between several individual cases after the evaluation of an expression.
Syntax:
switch (expression)
{
case v_1: statement_1; break;
case v_2: statement_2; break;
case v_3: statement_3; break;
...
case v_n: statement_n; break;
[default: statement_n+1];
}
|
- Switch Case statement evaluates the expression. If the expression value is equal to v_1 then run statement_1.
If the expression value is not equal to v_1, proceed to the next instruction statement_2 until to statement_n.
If the expression value is not equal to any value is going to default instruction statement_n +1.
Example:
<script type="text/javascript">
<!--
var city='New York';
switch (city)
{
case 'London': document.write("London");
break;
case 'Munich': document.write("Munich");
break;
case 'New York': document.write(New York");
break;
case 'Paris': document.write("Paris");
break;
case 'Vancouver': document.write("Vancouver");
break;
case 'Melbourne': document.write("Melbourne");
break;
default: document.write("Other city")
}
//-->
</script>
|