JS: difference between undefined and `undefined`

50 views Asked by At

Looking at this code; why doesn't a satisfy (a === typeof a)

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

There are 2 answers

0
jfriend00 On

Because:

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

One is a string with the string value 'undefined', one is the undefined primitive. Those two are not the same.

typeof x always returns string values such as "undefined", "boolean", "string", "object", etc....

0
vintagexav On