Note that it is not good programming practice to use
case expressions that contain side effects such as
function calls or assignments, because not all of the
case expressions are evaluated each time the
switch statement is executed. When side effects
occur only sometimes, it can be difficult to understand and predict
the correct behavior of your program. The safest course is simply to
limit your case expressions to constant
expressions.
As explained earlier, if none of the case
expressions match the switch expression, the
switch statement begins executing its body at the
statement labeled default:. If there is no
default: label, the switch
statement skips its body altogether. Note that in the earlier
examples, the default: label appears at the end of
the switch body, following all the
case labels. This is a logical and common place
for it, but it can actually appear anywhere within the body of the
statement.
The switch statement is implemented in JavaScript
1.2, but it does not fully conform to the ECMAScript specification.
In JavaScript 1.2, case expressions must be
literals or compile-time constants that do not involve any variables
or method calls. Furthermore, although ECMAScript allows the
switch and case expressions to
be of any type, JavaScript 1.2 and JavaScript 1.3 require that the
expressions evaluate to primitive numbers, strings, or boolean
values.