首页 > Web开发 > 详细

JS中的“==”符号及布尔值转换规则

时间:2014-09-13 00:38:34      阅读:496      评论:0      收藏:0      [点我收藏+]

what are the rules for how == converts types?

关于"=="的比较规则:

1. Comparing numbers and strings will always convert the strings to numbers. 

number类型与string类型比较,string会转换为number类型。如:2=="2" true

2. null and undefined will always equal each other.

null类型与undefined类型比较始终相等。如:var a = null,b; 则 a==b 为true。

3. Comparing booleans to any other type will always cause the booleans to be converted to numbers.

布尔类型与其他任何类型进行比较,布尔类型将会转换为number类型。如:var a = 0, b = false;则a==b为true

4. Comparing numbers or strings to objects will always cause the numbers or strings to be converted to objects.

number类型或者string类型与object类型进行比较,number或者string类型都会转换为object类型。如:var a = 0, b = {};则a==b为false

 

The rules for converting other types to booleans are actually relatively straightforward:

关于其他类型转换为布尔类型的规则:

1. undefined and null are always false.

2. Booleans are just treated as booleans (obviously).

3. Numbers are false if they equal 0 or NaN; otherwise, they’re true.

4. Strings are true, except for the empty string "", which is false.

5. Objects are always true.

JS中的“==”符号及布尔值转换规则

原文:http://www.cnblogs.com/ywxgod/p/3969316.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!