JS中undefined和null的几个区别

  1. undefined是个关键字,而null不是关键字
  2. null的本质是一个空的对象,而undefined其实是windows的一个属性,叫做未定义,它其实是一个已经定义了的属性,只不过这个属性的值叫做未定义。
  3. 当使用typeof 去查找的时候
     typeof(undefined)= “undefined”
     typeofnull= “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
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章