Reputation: 6062
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?
Upvotes: 398
Views: 163711
Reputation: 970
null
// This stands since the beginning of JavaScript
typeof null === 'object';
Short Answer:
This is a bug since the first release of ECMAScript which unfortunately can’t be fixed because it would break the existing code.
Explanation:
However, there is actually one logical explanation behind why null is an object in javascript.
In the initial version of JavaScript, values were stored in 32 bit units which consisted of a small type tag (1–3 bits) and the actual data of the value. The type tags were stored in the lower bits of the units. There were five of them:
000: object. The data is a reference to an object.
1: int. The data is a 31 bit signed integer.
010: double. The data is a reference to a double floating point number.
100: string. The data is a reference to a string.
110: boolean. The data is a boolean.
For all objects it was 000 as the type tag bits. null was considered to be a special value in JavaScript from its very first version. null was a representation of the null pointer. However, there were no pointers in JavaScript like C. So null simply meant nothing or void and was represented by all 0’s. Hence all its 32 bits were 0’s. So whenever the JavaScrit interpreter reads null, it considers the first 3 bits as type “object”. That is why typeof null returns “object”.
Note:
A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in
typeof null === 'null'.
Upvotes: 15
Reputation: 92
This is a bug remnant from javascript's first version.
"This is a bug and one that unfortunately can’t be fixed, because it would break existing code."
Reference and for more info: https://2ality.com/2013/10/typeof-null.html
Upvotes: 0
Reputation: 1223
++The author's answer is:
I think it is too late to fix typeof. The change proposed for typeof null will break existing code
It's too late. Ever since Microsoft created its own JavaScript engine and copied all the features and bugs of the first engine version, all subsequent engines copied this bug and now it's too late to fix it.
JS_TypeOfValue(JSContext *cx, jsval v)
{
JSType type = JSTYPE_VOID;
JSObject *obj;
JSObjectOps *ops;
JSClass *clasp;
CHECK_REQUEST(cx);
if (JSVAL_IS_VOID(v)) {
type = JSTYPE_VOID;
} else if (JSVAL_IS_OBJECT(v)) {
obj = JSVAL_TO_OBJECT(v);
if (obj &&
(ops = obj->map->ops,
ops == &js_ObjectOps
? (clasp = OBJ_GET_CLASS(cx, obj),
clasp->call || clasp == &js_FunctionClass)
: ops->call != 0)) {
type = JSTYPE_FUNCTION;
} else {
type = JSTYPE_OBJECT;
}
} else if (JSVAL_IS_NUMBER(v)) {
type = JSTYPE_NUMBER;
} else if (JSVAL_IS_STRING(v)) {
type = JSTYPE_STRING;
} else if (JSVAL_IS_BOOLEAN(v)) {
type = JSTYPE_BOOLEAN;
}
return type;
}
Upvotes: 1
Reputation: 350147
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
Upvotes: 1
Reputation: 2678
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!
Upvotes: 10
Reputation: 2204
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.
Upvotes: 0
Reputation: 15742
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. (reference)A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in
typeof null === 'null'
.
Upvotes: 319
Reputation: 1139
If
null
is a primitive, why doestypeof(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
Upvotes: 6
Reputation: 7519
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 withundefined
, 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 definesnull
as the primitive value that represents the intentional absence of any object value (ECMA-262, 11.4.11).
Upvotes: 36
Reputation: 359786
If
null
is a primitive, why doestypeof(null)
return"object"
?
Because the spec says so.
11.4.3 The
typeof
OperatorThe production UnaryExpression :
typeof
UnaryExpression is evaluated as follows:
- Let val be the result of evaluating UnaryExpression.
- If Type(val) is Reference, then
a. If IsUnresolvableReference(val) is true, return "undefined
".
b. Let val be GetValue(val).- Return a String determined by Type(val) according to Table 20.
Upvotes: 87