0

I just noticed that following expression is true in JavaScript:

typeof (null) == 'object'

That seems really weird, suppose I have a variable with null value and typeof returns object for this variable!

I want to know why typeof (null) is object in JavaScript?

Afshin Mehrabani
  • 33,262
  • 29
  • 136
  • 201

2 Answers2

1

It's been debated (see for example this discussion), and it's often seen as not really good, but it has a rationale : A null value takes the place of an object, it's the value you have when you expect an object and have none.

From ECMAScript :

null : primitive value that represents the intentional absence of any object value.

It's exactly the same as NaN being of type "number".

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Also, changing it now might break stuff. – Felix Kling Jul 10 '14 at 07:22
  • Of course. Personally I find it so logical and coherent that I almost like it like it is. Except I **never** found a case where it was useful to have `typeof null` being `"object"`... – Denys Séguret Jul 10 '14 at 07:25
  • But it's not the same as `NaN`. The IEEE 754 floating point standard describes a value `NaN` to represent an invalid result. So it really is of data type `number`, but its name just doesn't fit very well. But back to the topic, I really why there was proper operator built in that returns the true data type of a value. `typeof` is just messed up. Not sure right now what ES6 adds in this regard. – Felix Kling Jul 10 '14 at 07:27
  • The fact I see it as logically coherent and you don't, and that there's no definite argument, lets me think we should have closed the question as off-topic rather than as duplicate. – Denys Séguret Jul 10 '14 at 07:35
  • Depends on the "why" of the question. Is it "why does the operator behave like this" or "why was it decided that the operator behaves like this"? The former is answerable by pointing to the space. The latter would rather require an email to Brendan Eich. Anyways, there is no value in discussing this any further. – Felix Kling Jul 10 '14 at 07:39
  • Given the number of times this was discussed, Brendan would probably rather not receive another mail on this topic :) – Denys Séguret Jul 10 '14 at 07:39
  • Ehehe true :) It seems there is no search for http://esdiscuss.org/, that could probably give us an answer. – Felix Kling Jul 10 '14 at 07:40
-1

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the bogus typeof return value

reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
chf
  • 743
  • 4
  • 14
  • 4
    Did you just copy [this answer](http://stackoverflow.com/a/18808270/218196)? **edit:** Oh no you didn't, you just quote the same resource.... weird though. That's why it's good to mark quotes as such, like `> Quoted text goes here`. – Felix Kling Jul 10 '14 at 07:15
  • Both answers are copying part of the MDN article verbatim anyway... – Frédéric Hamidi Jul 10 '14 at 07:16
  • I just wanted to note that this answer doesn't actually deserve to be downvoted. As established, it just quotes MDN, although this could have been made clearer. – Felix Kling Jul 10 '14 at 07:23
  • I'm sorry and thanks for the edit. The next times i'll use quote. – chf Jul 10 '14 at 07:23
  • Yeah, it doesn't hurt to be explicit about it, like *"MDN says: > text*" – Felix Kling Jul 10 '14 at 07:24