我有一个变量,它可以是布尔 false
,也可以是整数(包括0)。我想把它放在一个开关语句中,比如:
switch(my_var){
case 0:
// Do something
break;
case 1:
// Do something else
break;
case false:
// Some other code
}
在我的 Google Chrome 测试中,它似乎运行得很完美,但是我对使用它有点紧张,因为我担心在某些浏览器中,如果 my_var
是 false
,它可能会执行自 0 == false
以来的第一个情况。
I'm just wondering if there is anything official in JavaScript that says the switch statement will use strict comparison such that 0 !== false
, but I can't find anything myself, and I'm not sure if this will work well in different JavaScript engines. Does anybody know if the comparison done by a switch statement is guaranteed to be strict?