Blog icon indicating copy to clipboard operation
Blog copied to clipboard

变量和类型 - Null 与 Undefined

Open logan70 opened this issue 5 years ago • 0 comments

Null 与 Undefined

  • null表示已被赋值,但值为空,即将一个变量显式赋值为null是正常的
  • undefined表示已声明还未定义的变量 或 对象上不存在的属性,故将变量或属性显式赋值为undefined是不正常的
  • nullundefined转换为布尔值均为false
  • null转换为数值为0undefined转换为数值为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

logan70 avatar Nov 16 '19 23:11 logan70