Inconsistency with null

# Sakthipriyan Vairamani (11 years ago)

console.log(typeof null); ​# object​

​console.log(Object.prototype.toString.call(null)); #​ ​ [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 es5.github.io/#x7.6.1.1, but when I do

​ ​var null = 1;

SyntaxError: Unexpected token null​

​It fails with the Syntax Error, since it is a Reserved Wordes5.github.io/#x7.6.1 .

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 ,

Sakthipriyan Vairamani

# Rick Waldron (11 years ago)

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

console.log(typeof null); ​# object​

​console.log(Object.prototype.toString.call(null)); #​ ​ [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.

This has been discussed extensively on this list over many years. Here is the last relevant strawman proposal: harmony:typeof_null

The TL;DR: typeof null is not going to change because it's too much of web-breaking risk.

Question 1: Why does it behave so? Is this something which has to be fixed?

No.

Also, null is not a keyword es5.github.io/#x7.6.1.1, but when I do

​ ​var null = 1;

SyntaxError: Unexpected token null​

​It fails with the Syntax Error, since it is a Reserved Wordes5.github.io/#x7.6.1 .

Question 2: What is the purpose of keeping null and boolean literals as "Reserved words" and not just keywords?

I apologize for answering this with another question, but what would the be the point in changing them to keywords?

# Andrea Giammarchi (11 years ago)

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

# Sakthipriyan Vairamani (11 years ago)

Rick,

Thanks for that URL. I will read that :-)

Regarding the second question, why do we even need something called reserved word? Reserved for what? Will it change in the future? If it is accepted in the mainstream usage, why to keep that as reserved?

  • Thanks and ,

Sakthipriyan

# Rick Waldron (11 years ago)

On Mon, May 12, 2014 at 12:06 AM, Sakthipriyan Vairamani < sakthiprofessional at gmail.com> wrote:

Rick,

Thanks for that URL. I will read that :-)

Regarding the second question, why do we even need something called reserved word? Reserved for what? Will it change in the future? If it is accepted in the mainstream usage, why to keep that as reserved?

There are several definitions for the word "reserve"; this is the one used by ECMAScript: en.wikipedia.org/wiki/Reserved_word ie. "reserved from use". That article also describes the different between a reserved word and a keyword.