5

I have learnt recently while debugging, that undefined is a data type and null is an object.

I think both of them comes under datatypes.

I checked the typeof undefined and typeof null. They returned "undefined" and "object" respectively.

typeof undefined 
"undefined"

typeof null
"object"

Could some body explain why is this strange behaviour.

3 Answers3

4

typeof null being object is an early mistake - when they tried correcting it in a Chrome nightly (to typeof null === "null" ) too many things relied on the current behavior and too much code broke.

JavaScript objects that are not set to normal values generally have three states:

  1. Undeclared.
  2. Undefined.
  3. Explicitly nothing.

Undeclared

For example - this case:

 y++; //I did not declare y before, this causes a reference error
 y === 'undefined'; //still a reference error 
 typeof y; //'undefined', since typeof is an operator and not a function.

Basically, an undeclared variable is in the state 'the script does not know this variable'.

Undefined

This means that the the runtime 'knows this variable' but it wasn't set to anything yet. Or, as the language specification puts this:

undefined value - primitive value used when a variable has not been assigned a value.

For example:

 var y; //y is undefined
 y === undefined; //true, y defined the line above
 typeof y; //undefined, but this is redundant, just do `=== undefined`
 (function(x){ /* x is undefined here as it was set to nothing*/})()

Explicitly nothing

When you have something that is supposed to have a value but you want to declare that it is nothing. Or, as the language specification puts this:

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

For example, document.getElementById("foo"); returns null if the element with the given ID is not in the DOM to indicate explicitly nothing was returned. Constrast this with functions that have no return statements so they return undefined which is the default.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
2

They have different types for the same reason that they both exist: undefined and null are conceptually not the same thing.

As this answer puts it:

undefined is the lack of a type and value, and null is the lack of a value.

But I prefer to apply a real-world analogy: matter vs dark matter.

undefined is like matter that does not exist. It has no form or substance, and therefore has no meaningful type. You set something to undefined when that thing should not be deemed as existing.

null, on the other hand, right across the realm of Computer Science, describes an object of an unknown state. It's still an object, but we don't know what object or what its value is. (In this sense it is similar to the numeric NaN which is still conceptually a number: we just don't know what number.) Nullity serves a placeholder for an unknown value, not for something that has no value. It's dark matter that does exist, in the form of an object, but no further information about its type is available since we don't have any information on it.

JavaScript understands this. So, historically, typeof null has been 'object' and typeof undefined has been 'undefined'.

Some people say this is a legacy bug since the dawn of JavaScript; I prefer to say that it's common sense from the dawn of JavaScript.

What is a bug in JavaScript is that null === null, and var o = {x:undefined}; 'x' in o; // true, neither of which should be the case.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    Worth mentioning is that 'those people' who say it's a legacy bug are the language creators. What you say is an interesting take on it, but it's simply not what the people who wrote the language meant or are working on it now think. It's also worth mentioning that it is not what the language specification states. I agree that `null !== null` would be a really interesting experiment, I don't see any case except `maybeNull === null` where `null === null` makes much sense in a high-level point of view. also note for example `o = {x:undefined}` means `'x' in o; // true` but `'y' in o; // false`. – Benjamin Gruenbaum Feb 13 '14 at 16:02
  • @BenjaminGruenbaum: Yes, I think the link under that sentence makes clear it is the working group who is of this opinion. However, it doesn't really matter to me who they are or what they created — I disagree with them either way. As for the latter example, I consider that to be a language bug; it should be `false` in both cases. – Lightness Races in Orbit Feb 13 '14 at 16:19
  • You don't have to like the language or any design choice the comittee made but it is what it is. I strongly encourage you to participate and voice your concerns and opinions :) – Benjamin Gruenbaum Feb 13 '14 at 16:23
  • @BenjaminGruenbaum: Then it's further worth mentioning that the ECMAScript creators did not invent the concept of nullity, nor of undefinedness. :) – Lightness Races in Orbit Feb 13 '14 at 16:27
0

In the documentation on MDN this is the reason they give for null being an object:

typeof null === 'object'; // This stands since the beginning of JavaScript

Yeah! That is a good reason... I guess they don't understand it themselves, and therefore a design error.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325