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語句中都會被自動轉(zhuǎn)為false
2、不同之處
undefined
undefined表示“缺少值”际跪,典型用法:
- 變量被聲明了迹卢,沒有賦值
- 調(diào)用函數(shù)時辖佣,應該提供的參數(shù)沒有提供
- 對象的屬性沒有賦值
- 函數(shù)沒有返回值
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規(guī)范的范圍。因為它是一個外部API具温,試圖獲取一個不存在的元素返回一個null值蚕涤,而不是undefined
Object.getPrototypeOf(Object.prototype) // null
埋點:
{}與 null,類型不同(引用類型铣猩、基礎類型)
NAN