JavaScript中undefined和null的幾個區別

  1. undefined是個關鍵字,而null不是關鍵字
  2. null的本質是一個“空”的對象,而undefined其實是windows的一個屬性,叫做未定義,它其實是一個已經定義了的屬性,只不過這個屬性的值叫做未定義。
  3. 當使用typeof 去查找的時候
     typeof(undefined)= “undefined”
     typeof(null) = “object”
    
    因此,===比較結果爲false
     null == undefined 					// true
     null === undefined 				// false
    
  4. 當共同轉換爲number時,null返回爲0,undefined返回爲NaN
    console.log(+null)						// 0
    console.log(+undefined)					// NaN
    console.log(2 + null)					// 2
    console.log(1 + undefined)				// NaN
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章