开发手册 欢迎您!
软件开发者资料库

JavaScript(JS) typeof,null和undefined

typeof 操作符来检测变量的数据类型。null是一个只有一个值的特殊类型。表示一个空对象引用。undefined一个没有设置值的变量。typeof 一个没有值的变量会返回undefined,本文主要介绍JavaScript(JS)中的typeof,null和undefined。

1、typeof 操作符

typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。

它返回值是一个字符串,该字符串说明运算数的类型。

例如,

// Numberstypeof 37 === 'number';typeof 3.14 === 'number';typeof Math.LN2 === 'number';typeof Infinity === 'number';typeof NaN === 'number'; // 尽管NaN是"Not-A-Number"的缩写typeof Number(1) === 'number'; // 但不要使用这种形式!// Stringstypeof "" === 'string';typeof "bla" === 'string';typeof (typeof 1) === 'string'; // typeof总是返回一个字符串typeof String("abc") === 'string'; // 但不要使用这种形式!// Booleanstypeof true === 'boolean';typeof false === 'boolean';typeof Boolean(true) === 'boolean'; // 但不要使用这种形式!// Symbolstypeof Symbol() === 'symbol';typeof Symbol('foo') === 'symbol';typeof Symbol.iterator === 'symbol';// Undefinedtypeof undefined === 'undefined';typeof declaredButUndefinedVariable === 'undefined';typeof undeclaredVariable === 'undefined'; // Objectstypeof {a:1} === 'object';// 使用Array.isArray 或者 Object.prototype.toString.call// 区分数组,普通对象typeof [1, 2, 4] === 'object';typeof new Date() === 'object';// 下面的容易令人迷惑,不要使用!typeof new Boolean(true) === 'object';typeof new Number(1) ==== 'object';typeof new String("abc") === 'object';// 函数typeof function(){} === 'function';typeof Math.sin === 'function';//NaNtypeof 1/0 === 'NaN';

2、null

null是JavaScript语言的关键字,它表示一个特殊值,常用来描述“空值”。对null执行typeof预算,结果返回字符串“object",那么也可以将null认为是一个特殊的对象值,它的含义是"非对象"。但实际上,通常认为null是它自有类型的唯一的一个成员,它可以表示数字、字符串、对象是“无值”的。

例如,

//处理null及'null'值function dealNull(obj){    for(var i in obj){        if(null == obj[i] || 'null' == obj[i]){            obj[i]='';        }else if('object' == typeof obj[i]){            dealNull(obj[i]);        }    }};//  测试的对象var a = {};a.aa = null;a.bb = 'null';a.c = 1;a.b = {};a.b.aaa = null;a.b.bbb = 'null';a.b.c = 1;a.array = [];a.array.push({    'a': null,    'b': 'null',    'c': 1});a.array.push({    'a1': null,    'b1': 'null',    'c1': 1});a.array.push({    'a2': null,    'b2': 'null',    'c2': 1});console.log(a);dealNull(a);console.log(a);

3、undefined

 在 JavaScript 中有 Undefined (type)、undefined (value) 和 undefined (variable)。

Undefined (type) 是 JavaScript 的内置类型。

undefined (value) 是 Undefined 类型的唯一的值。任何未被赋值的属性都被假定为 undefined(ECMA 4.3.9 和 4.3.10)。没有 return 语句的函数,或者 return 空的函数将返回 undefined。函数中没有被定义的参数的值也被认为是 undefined

var a;typeof a; //"undefined"window.b;typeof window.b; //"undefined"var c = (function() {})();typeof c; //"undefined"var d = (function(e) {return e})();typeof d; //"undefined"从 ECMA 3 开始,它可以被重新赋值,例如,undefined = "wonhero"; //把一个字符串赋值给 undefined (变量)typeof undefined //"string"f = undefined;typeof f; //"string"console.log(f); //"wonhero"

4、undefined 和 null 的区别

undefined 表示一个变量自然的、最原始的状态值,而 null 则表示一个变量被人为的设置为空对象,而不是原始状态。所以,在实际使用过程中,为了保证变量所代表的语义,不要对一个变量显式的赋值 undefined,当需要释放一个对象时,直接赋值为 null 即可。

nullundefined 的值相等,但类型不同。

例如,

console.log(typeof undefined)             // undefinedconsole.log(typeof null)                  // objectconsole.log(null === undefined)           // falseconsole.log(null == undefined)            // true