I read in Professional JavaScript for Web Developers by Nikolai Zakas in p. 78 of the third edition (the latter, I think):
The switch statement compares the values ββusing equal equal operators, so no type coercion occurs (for example, the string "10" is not equal to the number 10).
I made a few simple switch statements just for confirmation, and the result was different:
var num = "9"; switch (true) { case num < 0: alert("less than 0"); break; case num >= 0 && num <10: alert("between 0 and 10"); break; default: alert("False"); }
https://jsfiddle.net/pbxyvjyf/
Thus, forced enforcement is performed: alert("between 0 and 10") is selected. Have the rules changed or am I doing something wrong?
source share