【发布时间】:2014-04-14 09:09:11
【问题描述】:
我通常不喜欢使用 ==,但今天我只是在尝试以下代码,包括 ==,结果让我有点困惑。有人可以解释发生了什么吗?
所有这些都是假值:
'', 0, false, undefined, null
假设我这样做了:
if (undefined == null) {
alert('a');
} else {
alert('b');
}
以下陈述给出正确:
null == undefined
0 == ''
false == ''
0 == false
但是为什么下面的代码返回false?
undefined == 0
undefined == ''
null == 0
【问题讨论】:
-
您为什么希望他们返回
true? -
@liam 问题是关于“为什么”。
-
==进行类型转换,===没有。所以一个是真的,Javascript可以转换其他它不能的类型
标签: javascript casting comparison operators