You can do fall through in switch statement.
For fall through, include a comment indicating that the omission of the break statement is intentional:
switch (i) { case 25: /* falls through */ case 35: console.log("25 or 35"); break; case 45: console.log("45"); break; default: console.log("Other"); }
The switch statement works with all data types, including strings and objects.
The case values need not be constants; they can be variables and even expressions.
Consider the following example:
switch ("hello world") { case "hello" + " world": console.log("hello."); break; case "hi": console.log("hi."); break; default: console.log("Unexpected message was found."); }
In this example, a string value is used in a switch statement and the case statement involve calculation.
You can also use case expressions to compare values:
var num = 25; switch (true) { case num < 0: console.log("Less than 0."); break; case num >= 0 && num <= 10: console.log("Between 0 and 10."); break; case num > 10 && num <= 20: console.log("Between 10 and 20."); break; default: console.log("More than 20."); }
Here, a variable num is defined outside the switch statement.
The expression passed into the switch statement is true, because each case is a conditional statement.
Each case is evaluated in order until a match is found or until the default statement is encountered.
The switch statement compares values using the identically equal operator, so no type coercion occurs.
For example, the string "10" is not equal to the number 10.