JS中Null与Undefined的区别

JS中Null与Undefined的区别

在JavaScript中存在这样两种原始类型:Null与Undefined。这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined?

Undefined类型只有一个值,即undefined。当声明的变量还未被初始化时,变量的默认值为undefined。
Null类型也只有一个值,即null。null用来表示尚未存在的对象,常用来表示函数企图返回一个不存在的对象。

  1. var oValue;  
  2. alert(oValue == undefined); //output "true"  

这段代码显示为true,代表oVlaue的值即为undefined,因为我们没有初始化它。


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

当页面上不存在id为"notExistElement"的DOM节点时,这段代码显示为"true",因为我们尝试获取一个不存在的对象。


  1. alert(typeof undefined); //output "undefined"  
  2. alert(typeof null); //output "object"  

第一行代码很容易理解,undefined的类型为Undefined;

第二行代码却让人疑惑,为什么null的类型又是Object了呢?其实这是JavaScript最初实现的一个错误,后来被ECMAScript沿用下来。在今天我们可以解释为,null 即是一个不存在的对象的占位符,但是在实际编码时还是要注意这一特性。

  1. null == undefined); //output "true"  


ECMAScript认为undefined是从null派生出来的,所以把它们定义为相等的。但是,如果在一些情况下,我们一定要区分这两个值,那应该怎么办呢?可以使用下面的两种方法。

  1. alert(null === undefined); //output "false"  
  2. alert(typeof null == typeof undefined); //output "false"  


使用typeof方法在前面已经讲过,null与undefined的类型是不一样的,所以输出"false"。而===代表绝对等于,在这里null === undefined输出false。

-------------------------------------------------------------------------------------------------------------------------------------------------------

在javascript中什么情况出现下面三种情况: null undefined  " "
文件1:null_undefined.html     文件2:null_undefined.js 

null_undefined.html  
null_undefined.js 
var test = function() 

alert(window.aiter2008); //弹出undefined window空间中没有aiter2008这个变量 
alert(document.getElementById("test_text1111")); //null 在上面的null_undefined.html文件中没有test_text1111元素 
var testText = document.getElementById("test_text").value; // test_text输入框中没有输入任何元素 
alert(testText==""); //true 
alert(testText==null); //false 

这次测试可以得到结论: 
一个对象中没有指定的变量,而要使用,会出现 undefined 
Html中没有的元素,通过document.getElementById("")查找的结果为: null 
Html中有的元素,但是没有任何值,通过document.getElementById("")查找的结果为:” ”; 不是null 

2

 null_undefined.html 文件不变 
null_undefined.js 

var test = function() 

var testText = document.getElementById("test_text"); 
var testName = testText.name; 
var testUn = testText.name11; 
alert("test_text's name::"+testName); // test_text's name:: test_text 
alert("test_text's name11:::"+testUn); // test_text's name11:::undefined 


比较: "==="和"=="的区别

"===":类型和值一起比较,"==":只比较值


null
在javascript中表示“无值”。null常常被看做对象类型的一个特殊值,即代表“无对象”的值。null是个独一无二的值,有别与其他所有的值。如果一个变量的值为null,那么就说明它的值不是有效的对象、数组、数字、字符串和布尔值。

undefined
还有一种特殊值Javascript会偶尔一用,它就是值undefind。在使用了一个并未申明的变量时,或者是使用已经申明但是并没有赋值的变量时,又或者是使用了一个并不存在的的变量和属性时,返回的就是这个值。
注意这个特殊的undefined值不同于null。

 

结论 
一个对象没有的属性,调用会出现: undefied 这和调用window的aiter2008属性一样
详细出处参考:http://www.jb51.net/article/13202_2.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值