Javascript’s null Value Has an Identity Crisis
I came across another identity crisis in Javascript while working on my $class library. This time, it is the null value that needs help. Observe the output of the following expressions:
This time, all browsers agree: null is an “object”, but not an instance of Object. What does it mean? The way I see it, you can not use null as an Object, so it should have no relation to Object at all. You cannot access or create properties on null, you cannot evaluate null.toString(), etc.
My solution in the $class library library is similar to what I’ve done for undefined. I created a dummy class called Null and put a special case in the $class.instanceOf and $class.getClass methods to consider the null value to be an instance of Null and nothing else.
July 5th, 2006 at 6:50 am
The browsers act exactly as it is described in the ECMA 262 standard (http://www.ecma-international.org/publications/standards/Ecma-262.htm).
1. Section 4.3.1 defines what a type is: A type is a set of data values.
2. Section 4.3.3 defines the Object type: An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.
3. Section 4.3.12 defines the Null type: The type Null has exactly one value, called null.
Based on the above and the assumption that a value can belong only to one type we can conclude that
null instanceof Objectmust befalsebecausenullis not part of theObjecttype (or set). If there was a native type (or object) likeNullthennull instanceof Nullwill betrue.There are more “evidences” in the standard, however:
1. Section 11.4.3 defines the
typeofoperator. It explicitly defines thattypeof nullis “object”.2. Section 11.8.6 defines the
instanceofoperator. Basicaly it says that the result is what is returned by the[[HasInstance]]internal method of the right-hand operand.3. The only standard object that implements the
[[HasInstance]]method is theFunctionobject. Section 15.3.5.3 defines this method. It explicitly says that if the value of the left-hand operand of theinstanceofoperator (which was passed as an argument to this method) isnullthen the result isfalse.I do see reasoning in the fact that
nullis not instance of any object. The same is in other languages (like for example Java) wherenull instanceof Objectis alsofalse.Let’s say that you have a function that works on Number or String objects. For example:
function reverseSymbols(o)
{
if (o instanceof Number) {
// convert the number to string and reverse it
} else if (o instanceof String) {
// reverse the string
}
}
I think it is obvious that in this case it is a good thing that
null instanceof Objectwill returnfalse.July 6th, 2006 at 12:17 am
I agree that the browsers are acting according to standards in this situation. The problem is that the behavior creates a conflict that is a pain in the butt when programming. I agree with the behavior of the instanceof operator; null is not an instance of Object and it should not be. It is the typeof operator that I am complaining about. Since null is not an instance of Object, it makes no sense that typeof null == “object”. It would make more sense if typeof null == “null”. I can’t think of any reason that the behavior of the typeof operator as defined by the standards would be desireable. I know that in other languages (specifically, Java), a null value can have a type (e.g. String myString = null;), but since Javascript is so loosly typed, null is just null (although internally to the implementation of the language it is a value of type Null, that info is not available to the end-user of the language).
btw - I think you may have left something out of your example that shows why “null instanceof Object == true” would be bad, because the example never tests if “o instanceof Object”.
June 13th, 2007 at 12:08 pm
I totally agree with UP that since null is considered not to be an instance of Object, then logically null should not have a type of ‘object’. In fact in Professional JavaScript for Web Developers - 2005 - (By Nicholas Zachas) it is written at page 17 (I know.. it sounds almost religious…):
“You may wonder why the typeof operator returns “object” for a value that is null.
This was actually an error in the original JavaScript implementation that was then
copied in ECMAScript. Today, it is rationalized that null is considered a placeholder
for an object, even though, technically, it is a primitive value.”
So I guess UP is right, it is actually an error.
September 1st, 2007 at 9:28 am
As I understand it, this is basically a bug in the original Netscape JavaScript implementation that later was promoted to feature — Microsoft had copied the behaviour in their implementation and was hestitant to break existing code.
(Things like this really suck about JS — and as I understand it ECMAScript 4 isn’t going to fix this.)