dongpublic 发表于 2013-1-29 08:38:16

javaScript 全等(===)和 等于(==)的差别

javaScript比较有两组相等比较符号 : == & !=   === & !===
下面的 == 比较时的一些特别的地方:
console.log('' == false);// true
console.log('' == 0);// true
console.log('\t\n' == 0);//true
console.log(null == undefined);// true
console.log(0 == false);// true
console.log(0 == '0')         // true
console.log('0' == false);// true
而换成===比较后,就都为false
其实在 == 比较的时候是对转换类型了,而 === 只比较值了,
规则如下:
如果比较的两者中有bool,会把 bool 先转换为对应的 number,即 0 和 1

如果比较的双方中有一方为number一方为string,会把string转换为数字

把string直接转换为bool的时候,空字符串''转换为 false,除此外的一切字符串转换为 true
详细请查看附件 ECMA-262 文件的 11.9.3和11.9.6

http://dl.iteye.com/upload/attachment/0072/4689/bdd5a48a-d887-3e2d-9175-f52710e6a21a.png

http://dl.iteye.com/upload/attachment/0072/4691/2b4df634-7ae4-3f5d-91a0-c0ea2a512b75.png

http://dl.iteye.com/upload/attachment/0072/4693/9166fa08-49a2-3138-953b-f55a37a7954c.png
页: [1]
查看完整版本: javaScript 全等(===)和 等于(==)的差别