码迷,mamicode.com
首页 > 其他好文 > 详细

Null 和 undefined 的区别

时间:2018-04-20 13:34:46      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:函数   void   oid   声明   style   div   ...   null   color   

null 表示一个值被定义了,定义为“空值”

undefined 表示根本不存在定义。

1:

所以设置一个值为null 是合理的, obj.value = null;  正确

设置一个值为undefined是不合理的  obj.value = undefined; 错误

2:

null预定义为一个object 值为空。 undefined预定义为全局变量,值为undefined。

1 typeof null; //object
2 typeof undefined; // undefined

3:

转义 

1 !!(null);//false
2 !!(undefined);//false
3 Number(null);// 0
4 Number(undefined);// NaN
5 null == undefined; // true
6 null === undefined; // false

4:

判定

1 isNull = function (obj){ return obj === null;}
2 isUndefined = function(obj){ return obj === void 0;}

5:

用法

5.1 

 1  null 常用来定义一个空值
 2 
 3  undefined :
 4 5.1变量声明了未赋值 
 5 var test;
 6 console.log(test);// undefined
 7 
 8 5.2 调用函数时,没提供应该提供的参数,参数为undefined
 9 function(lists){...}// lists 未提供
10 
11 5.3 对象没有赋值的属性,该属性为undefined
12 var test={};
13 console.log(test.aaa);//undefined
14 5.4 函数没有返回值时,返回undefined
15 function test(){}
16 test();//undefined

 

Null 和 undefined 的区别

标签:函数   void   oid   声明   style   div   ...   null   color   

原文地址:https://www.cnblogs.com/ming-os9/p/8889657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!