Javascript TUTORIAL
Switch statement type matching
If the parameter of the switch statement is string and the parameter of the case is integer, they do not match. As an example consider the following code
|
- If the string defined
var height = "61";
would have matched with the
case 61 :
It would have produced an output "height is more than 5 feet". Instead it gives the output "height is out of 59 inch to 61 inches". The reason is simple - the types do not match. This happens because switch uses ==== in place of == when comparing the values.
You can try this example online here.
Javascript switch different types
A way to fix this is either change
var height = "61";
to
var height = "1;
or change
case 61 :
to
case 61 :