学习笔记:JS中undefined和nulll的区别

参考引用:
https://www.cnblogs.com/ly0612/p/6696982.html
Null与Undefined都是JS中的原始类型,区别是什么?

定义

  • 当声明了一个变量,但是没有初始化,得到的就是undefined。没有返回值的函数返回为undefined,没有实参的形参也是undefined。它是一个预定义的全局变量。
var oValue;  
alert(oValue == undefined); //output "true" 
  • null表示空对象指针,一个不存在的对象

    alert(null == document.getElementById('notExistElement'));  

当试图获取一个不存在的DOM节点时,这段代码显示为true

typeof

  • undefined的类型为undefined
  • null的类型为Object
alert(typeof undefined); //output "undefined"  
alert(typeof null); //output "object" 

==

ECMAScript认为undefined是从null派生出来的,所以把它们定义为相等的。


    alert(null == undefined); //output "true"  

=== 则为false

posted @ 2019-08-29 16:13  闲敲棋子  阅读(416)  评论(0编辑  收藏  举报