Inconsistency with null

Andrea Giammarchi andrea.giammarchi at
Sun May 11 20:56:39 PDT 2014

`null` has been historically ambiguous but because of that, changing its
`typeof` result broke code almost everywhere. Libraries not so quick or
ready to embrace this difference, books talking about `if (typeof obj ===
'object' && obj)` in every bloody example.

However, I always found fascinating the fact that
`Object.getPrototypeOf(Object.prototype)` is indeed `null` ... so if every
object indirectly inherits from `null`, why wouldn't `null` be indeed the
root of all objects and then, due inheritance, being the root of the
`typeof value === 'object'` ?

If you create any other inheritance through a `prototype`, `"[object
ConstructorName]"` is what you get.

Think about the root of all JS objects as `function Null() {}
Null.prototype = Object.create(null)` and consider such value same as
`Empty` is the hidden root of all `Function` instances, including the
`Object` constructor one.

Why would you redefine `null` as variable is a mystery to me, and I love to
believe and trust the fact nobody can.

`undefined` has been the most doomed variable ever, where `var undefined =
true` in the root of any script would have create so many pitfalls in any
code trusting such value that you start thinking `value == null` is the
only way to compare against `undefined` itself, at least in ES3, and also
in every closure based env since `window.undefined` is safe even under
`strict` but as variable can be redefined in any _AMD_ like loader ...

As summary, and in my opinion, `null` is good as it is.

If you want to use `typeof` to deal with it consider the double check,
otherwise consider that `Object.prototype.toString` not only saves you from
`null` mistakes you might have for some reason, it also saves you from `new
Number` and `new String` ... well, it won't save you from `new Boolean`
since knowing it's a boolean but checking without passing through
`valueOf()` won't ever be `false` ... you got the idea, for anything else

On Sun, May 11, 2014 at 8:43 PM, Sakthipriyan Vairamani <
sakthiprofessional at> wrote:

> console.log(typeof null);
> ​# object​
> ​console.log(;
> #​
> ​ [object Null]​
> ​This means that null​
> ​ is expected to be an object. But,
>> ​console.log(Object.keys(null));​
> ​# TypeError: Object.keys called on non-object​
>  ​The error message says that null​
> ​ is a non-object. It contradicts the result of typeof and toString.
> *Question 1:* Why does it behave so? Is this something which has to be
> fixed?
> Also, null is not a keyword <>, but when I
> do
>> ​var null = 1;
> # SyntaxError: Unexpected token null​
> ​It fails with the Syntax Error, since it is a Reserved Word<>
> .
> *Question 2:* What is the purpose of keeping null and boolean literals as
> "Reserved words" and not just keywords?
> Note: I tried these samples in node v0.11.13-pre
> - Thanks and Regards,
> Sakthipriyan Vairamani
> _______________________________________________
> es-discuss mailing list
> es-discuss at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list