javascript之 == vs ===

[size=medium][b]一、Comparison Overview[/b][/size]

[b]1. Speed Compare(运行速度比较):[/b]


=== will never be slower than ==.
They both do type checking, so === doesn't do anything extra compared to ==,
but the type check may allow === to exit sooner when types are not the same.


[b]2. How do they work(它们是如何工作的?):[/b]

[b]=== [/b]:
compare both type and value

[b]==[/b] :
only compare value.
if there are different types, then convert the value.


[b]Example:[/b]


if(true == 1); //true, because 'true' is converted to 1 and then compared
if(true === 1); // false

if("4" == 4); //true, because "4" is converted to 4 and then compared
if("4" === 4); // false

if(true == 2); //false, because 'true' is converted to 1



[b]Exception:[/b]

var a = new String("abc");
var b = "abc";

if(a==b); // true
if(a===b); // false, because typeof a is object, type of b is String




[size=medium][b]二、How they work?[/b][/size]

[b]1、How "===" compare values?[/b]

[b]"[color=green]Type Checking[/color]"[/b] is the first step !
[quote]
[color=olive]
[b]11.9.6 The Strict Equality Comparison Algorithm[/b]
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
4. If Type(x) is not Number, go to step 11.
5. If x is NaN, return false.
6. If y is NaN, return false.
7. If x is the same number value as y, return true.
8. If x is +0 and y is −0, return true.
9. If x is −0 and y is +0, return true.
10. Return false.
11. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
12. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false.
[/color]

http://stackoverflow.com/a/1813267/2893073

[/quote]


[b]2. When use "==", how is the converstion?(不同类型的值如何转换?)[/b]


[img]http://dl2.iteye.com/upload/attachment/0117/9947/10164c10-b856-33c4-b8c6-212f52d440b1.png[/img]


[img]http://dl2.iteye.com/upload/attachment/0117/9949/e4a22fdb-a551-35d0-a0a8-8e25319b0258.png[/img]


[img]http://dl2.iteye.com/upload/attachment/0117/9951/f0c584a2-f7f4-3d38-bc44-bca14ff95f2c.png[/img]


[size=medium][b]三. Summary(总结)[/b][/size]

Always use 3 equals unless you have a good reason to use 2.


[b]Reference:[/b]

http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons

http://dorey.github.io/JavaScript-Equality-Table/


-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值