1、相似性
if (!undefined) console.log('undefined is false'); // undefined is false if (!null) console.log('null is false'); // null is false undefined == null// true
undefined和null在if語句中都會被自動轉為false
2、不同之處
undefined
undefined表示“缺少值”,典型用法:
- 變量被聲明了,沒有賦值
- 調用函數時,應該提供的參數沒有提供
- 對象的屬性沒有賦值
- 函數沒有返回值
var i; i // undefinedfunction
f(x){console.log(x)} f() // undefined
var o = new Object(); o.p // undefinedr
x = f(); x // undefined
NULL
- 作為原型鏈的終點
- DOM,它是獨立于語言的,不屬于ECMAScript規范的范圍。因為它是一個外部API,試圖獲取一個不存在的元素返回一個null值,而不是undefined
Object.getPrototypeOf(Object.prototype) // null
埋點:
{}與 null,類型不同(引用類型、基礎類型)
NAN