Blog
Blog copied to clipboard
变量和类型 - Null 与 Undefined
Null 与 Undefined
-
null
表示已被赋值,但值为空,即将一个变量显式赋值为null
是正常的 -
undefined
表示已声明还未定义的变量 或 对象上不存在的属性,故将变量或属性显式赋值为undefined
是不正常的 -
null
和undefined
转换为布尔值均为false
-
null
转换为数值为0
,undefined
转换为数值为NaN
-
null == undefined
,因为ECMAScript
定义如此,并没有发生隐式类型转换ECMAScript中定义 "If x is null and y is undefined, return true."
详见ECMAScript#abstract-equality-comparison -
null !=== undefined
,二者不是同一数据类型
// bad
let a = undefined
// good
let a