有人可以帮忙解释一下吗?我理解的是 === 会进行精确匹配,但是与 null 进行比较时具体意味着什么呢?
有人可以帮忙解释一下吗?我理解的是 === 会进行精确匹配,但是与 null 进行比较时具体意味着什么呢?
a
的值为null
,那么a === null
将会是true。因此,只有当1. 如果
Type(x)
与Type(y)
不同,则返回false。
2. 如果Type(x)
是Undefined,则返回true。
3. 如果Type(x)
是Null,则返回true。
Type(a)
为Null时,比较才会返回true。Type
函数与typeof
运算符混淆。typeof null
实际上会返回字符串"object"
,这更令人困惑而不是帮助。
a == null
如果变量 a
的值为 null
或 undefined
,则返回 true。2. 如果
x
为null
并且y
为undefined
,则返回 true。
3. 如果x
为undefined
并且y
为null
,则返回 true。
===
是一个严格的运算符,它不仅比较值,还比较变量的类型。
string===string
int===int
==
只比较值。
=== 表示它会检查变量的值和类型。例如从 w3c 页面中提取,给定 x = 5,x 是一个 int,所以 x==="5" 是 false,因为它比较的是 int 和 string,而 x ===5 是 true,因为它既是 int 类型又是正确的值。
使用三个等号,值必须在类型上相等。但不适用于 ==。
i.e
1==true // this return true
1===true // but return false
a==null // will true if a is null or undefined
1==true
将会是 true
但是 1===true
将会是 false
例如,===
是在数据类型级别上进行比较,而使用 ==
时,JavaScript 会自动进行类型转换
undefined === null
是false
,undefined == null
是true
-> 在大多数情况下,您会使用==
来同时处理null
和undefined
。 - Vedran