null與undefined均可以表示沒有、無的意思,但它們之間仍是有區別。spa
undefined:當var申明變量,可是未初始化時,對象爲undefined。code
例如:對象
var animal; console.log(animal); //undefined
同時:blog
var animal; console.log(animal == undefined); //true
null:null是一個特殊的值。console
var animal = null; console.log(animal); //null console.log(animal == null);//true
typeof(返回一個值的數據類型)做用下:class
var animal; console.log(typeof animal); //undefined console.log(typeof undefined); //undefined var pig = null; console.log(typeof pig); //object console.log(typeof null); //object
可見null變量
可是若是這樣:object
console.log(undefined == null); //true
Number(非數值轉換成數值)做用下數據類型
console.log(Number(null)); //0 console.log(Number(undefined));//NaN
!邏輯非操做im
console.log(!null);//true console.log(!undefined);//true
&&邏輯與操做
var dog; console.log(dog && null);//null console.log(dog && undefined);//undefined var dig = null; console.log(dig && null);//null console.log(dig && undefined);//null var pig = "pig"; console.log(pig && null);//null console.log(pig && undefined);//undefined