<SCRIPT LANGUAGE="JavaScript"> var a = 0; var b = -1; var c = 1; function assert (aVar) { if (aVar==true) alert(true); else alert(false); } assert(a) ; assert(b) ; assert(c) ; </SCRIPT>
運行代碼框 [Ctrl+A 全部選擇 提示:你可先修改部分代碼,再按運行]
按照我的理解,任何非0的數(shù)值的布爾值都應該為true。
可是這道題的正確輸出為:false false true。
(-1==true)的值為false。
再來看下面這個例子:
<SCRIPT LANGUAGE="JavaScript"> var a = 0; var b = -1; var c = 1; function assert (aVar) { if (aVar) alert(true); else alert(false); } assert(a) ; assert(b) ; assert(c) ; </SCRIPT>
6.If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y. 7.If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
<SCRIPT LANGUAGE="JavaScript"> var a = "undefined"; var b = "false"; var c = ""; function assert (aVar) { if (aVar==true) alert(true); else alert(false); } assert(a); assert(b); assert(c); </SCRIPT>
4.If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y). 5.If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.