JS:undefined和`undefined`之间的区别

时间:2015-06-15 21:38:31

标签: javascript undefined typeof

看看这段代码;为什么不满足(a === typeof a)

var a;
(a === undefined)?console.log("a is undefined"):null;
(typeof a === 'undefined')?console.log("typeof a is 'undefined'"):null;

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

由于:

var a;
typeof a === 'undefined';
a === undefined;

一个是字符串值为'undefined'的字符串,一个是undefined原语。那两个不一样。

typeof x始终返回字符串值,例如"undefined""boolean""string""object"等....