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.
The JavaScript specification says about null : null is a primitive value that represents the intentional absence of any object value. If you see null (either assigned to a variable or returned by a function), then at that place should have been an object, but for some reason, an object wasn't created.
The type of NaN , which stands for Not a Number is, surprisingly, a number. The reason for this is, in computing, NaN is actually technically a numeric data type. However, it is a numeric data type whose value cannot be represented using actual numbers.
The null in TypeScript is a special value & also a data type. The value null represents the intentional absence of any object value. It is one of TypeScript's primitive values and is treated as falsy for boolean operations.
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'
.
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.
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).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With