js中null, undefined 和 typeof

参考自:http://www.cnblogs.com/wicub/p/3442891.html

 

typeof 是运算符,注意不是函数,是运算符,其作用,是考察变量究竟是什么类型。或曰,是变量是否定义或是否初始化的照妖镜。返回值是字符串。

undefined 表示一个对象没有被定义或者没有被初始化。

null 表示一个尚未存在的对象的占位符。

 

首先做四个测试:

 1         //测试1: 变量没有定义时,只能使用typeof
 2 
 3         //console.log('a == undefined: ' + a == undefined);             //报错
 4         //console.log('a == null: ' + a == null);                       //报错
 5         //console.log('a === undefined: ' + a === undefined);           //报错
 6         //console.log('a === null: '+ a===null);                        //报错
 7         console.log('typeof a == undefined: ' + (typeof a == undefined)); //false
 8         console.log('typeof a == \'undefined\': ' + (typeof a == 'undefined')); //true
 9         console.log('typeof a === \'undefined\': ' + (typeof a === 'undefined'));   //true
10         console.log(typeof a);                      //undefined
11 
12         //测试2:变量有定义,但未初始化,typeof,undefined,null都可以使用
13         var b;
14         console.log('b == undefined: ' + (b == undefined));             //true
15         console.log('b == null: ' + (b == null));                       //true
16         console.log('b === undefined: ' + (b === undefined));           //true
17         console.log('b === \'undefined\': ' + (b === 'undefined'));     //false
18         console.log('b === null: '+ (b===null));                        //false
19 
20         console.log('typeof b == undefined: ' + (typeof b == undefined)); //false
21         console.log('typeof b == \'undefined\': ' + (typeof b == 'undefined')); //true
22         console.log('typeof b === \'undefined\': ' + (typeof b === 'undefined'));   //true
23         console.log(typeof b);                      //undefined
24 
25         //测试3:变量有定义且已经初始化
26         b = 0;
27         console.log('b == undefined: ' + (b == undefined));             //false
28         console.log('b == null: ' + (b == null));                       //false
29         console.log('b === undefined: ' + (b === undefined));           //false
30         console.log('b === \'undefined\': ' + (b === 'undefined'));     //false
31         console.log('b === null: '+ (b===null));                        //false
32 
33         console.log('typeof b == undefined: ' + (typeof b == undefined)); //false
34         console.log('typeof b == \'undefined\': ' + (typeof b == 'undefined')); //false
35         console.log('typeof b === \'undefined\': ' + (typeof b === 'undefined'));   //false
36         console.log(typeof b);                      //number
37 
38         //测试4: 变量是函数参数
39         function test(b){
40             
41             console.log('b == undefined: ' + (b == undefined));             //true
42             console.log('b == null: ' + (b == null));                       //true
43             console.log('b === undefined: ' + (b === undefined));           //true
44             console.log('b === \'undefined\': ' + (b === 'undefined'));     //false
45             console.log('b === null: '+ (b===null));                        //false
46 
47             console.log('typeof b == undefined: ' + (typeof b == undefined)); //false
48             console.log('typeof b == \'undefined\': ' + (typeof b == 'undefined')); //true
49             console.log('typeof b === \'undefined\': ' + (typeof b === 'undefined'));   //true
50             console.log(typeof b);                      //undefined
51         }
52         test();

 

 

null和undefined的设计初衷:

  1. undefined:表示一个对象没有被定义或者没有被初始化。
  2. null:表示一个尚未存在的对象的占位符。
  undefined和null是相等的。有:
        console.log(undefined == null);     //true
        console.log(undefined === null);    //false

 


未声明的对象只能用typeof运算符来判断!!否则会报错
 1         console.log(undefined == null);     //true
 2         console.log(undefined === null);    //false
 3 
 4         console.log(typeof undefined);      //undefined
 5         console.log(typeof null);           //object
 6         console.log(typeof "string");       //string
 7         console.log(typeof 0);              //number
 8         console.log(typeof function(){});   //function
 9         console.log(typeof true);           //boolean
10         console.log(typeof {});             //object
11 
12         console.log(typeof null == 'null'); //false     null类型返回object,这其实是JavaScript最初实现的一个错误,然后被ECMAScript沿用 了,也就成为了现在的标准。所以需要将null类型理解为“对象的占位符”,就可以解释这一矛盾,虽然这只是一中 “辩解”。对于代码编写者一定要时刻警惕这个“语言特性”

 

posted on 2016-08-13 15:56  杠子  阅读(554)  评论(1编辑  收藏  举报

导航