switch
vs. C and C++'s switch
How do you use the switch statement in javascript? Is it used the same way it is in C++ (with the case statement)?
Yes, it is exactly the same, with three exceptions:
In C and C++, all of the case
values must be the same type as the switch
value.
In JavaScript, there is no such restriction.
In C and C++, you cannot switch
on the contents of a string.
In JavaScript, there is no such restriction.
In C and C++, all case
values must be unique.
In JavaScript, there is no such restriction. The first matching case
value is recognized, regardless of duplicates.
Presently, neither language will let you use variables for the case
values.
Charlton Rose
28 August 1997