Why is typeof null "object"?
JavascriptPrimitiveJavascript Problem Overview
I'm reading 'Professional Javascript for Web Developers' Chapter 4 and it tells me that the five types of primitives are: undefined, null, boolean, number and string.
If null
is a primitive, why does typeof(null)
return "object"
?
Wouldn't that mean that null
is passed by reference (I'm assuming here all objects are passed by reference), hence making it NOT a primitive?
Javascript Solutions
Solution 1 - Javascript
From the MDN page about the behaviour of the typeof
operator:
null
// This stands since the beginning of JavaScript typeof null === 'object';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 "object"typeof
return value. (http://www.2ality.com/2013/10/typeof-null.html">reference</a>)</p>A fix was proposed for ECMAScript (via an opt-in), but was"">https://web.archive.org/web/20160331031419/http://wiki.ecmascript.org:80/doku.php?id=harmony:typeof_null">was rejected. It would have resulted in
typeof null === 'null'
.
Solution 2 - Javascript
> If null
is a primitive, why does typeof(null)
return "object"
?
Because the spec says so.
>### 11.4.3 The typeof
Operator
> The production UnaryExpression : typeof
UnaryExpression is evaluated as follows:
>
> 1. Let val be the result of evaluating UnaryExpression.
> 2. If Type(val) is Reference, then
> a. If IsUnresolvableReference(val) is true, return "undefined
".
> b. Let val be GetValue(val).
> 3. Return a String determined by Type(val) according to Table 20.
>
>
Solution 3 - Javascript
As has been pointed out, the spec says so. But since the implementation of JavaScript predates the writing of the ECMAScript spec, and the specification was careful not to correct foibles of the initial implementation, there's still a legitimate question about why it was done this way in the first place. Douglas Crockford calls it a mistake. Kiro Risk thinks it kinda sorta makes sense:
> The reasoning behind this is that null
, in contrast with undefined
, was (and still is) often used where objects appear. In other words, null
is often used to signify an empty reference to an object. When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return "object". In fact, the ECMAScript specification defines null
as the primitive value that represents the intentional absence of any object value (ECMA-262, 11.4.11).
Solution 4 - Javascript
From the book YDKJS > This is a long-standing bug in JS, but one that is likely never going > to be fixed. Too much code on the Web relies on the bug and thus > fixing it would cause a lot more bugs!
Solution 5 - Javascript
> If null
is a primitive, why does typeof(null)
return "object
"?
in short: it is bug in ECMAScript, and the type should be null
reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null
Solution 6 - Javascript
In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object. It should be null.
Solution 7 - Javascript
In JavaScript, typeof null is 'object', which incorrectly suggests that null is an object. This is a bug and one that unfortunately can't be fixed, because it would break existing code.
Solution 8 - Javascript
For people who interested in some code that made this behaviour this is the link for you guys....
Solution 9 - Javascript
The ECMAScript specification identifies these language data types:
> 6.1.1 The Undefined Type
> 6.1.2 The Null Type
> 6.1.3 The Boolean Type
> 6.1.4 The String Type
> 6.1.5 The Symbol Type
> 6.1.6 Numeric Types
> 6.1.6.1 The Number Type
> 6.1.6.2 The BigInt Type
> 6.1.7 The Object Type
For historical reasons the typeof
operator is not consistent with this categorisation in two cases:
typeof null == "object"
: this is unfortunate, but something we have to live with.typeof
of a function object evaluates to "function", even though according to the specification it has as data type Object.
Another operator -- instanceof
-- can be used to know whether an object inherits from a certain prototype. For instance, [1,2] instanceof Array
will evaluate to true.
One way to determine whether a value is an object, is to use the Object
function:
if (Object(value) === value) // then it is an object; i.e., a non-primitive