![]() To draw a parallel here, consider typeof(NaN) = "number". 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). When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return "object". In other words, null is often used to signify an empty reference to an object. The reasoning behind this is that null, in contrast with undefined, was (and still is) often used where objects appear. The answer might disappoint some, but the truth is simply because the table above says to do so. What's happening here? > typeof null = "object" // true ![]() Here is the relevant portion (although I would suggest you read the whole post): This page has a nice description of the history here surrounding why typeof(null) gives "object": It would have resulted in typeof null = 'null'. ( reference)Ī fix was proposed for ECMAScript (via an opt-in), but was rejected. Consequently, null had 0 as type tag, hence the "object" typeof return value. null was represented as the NULL pointer (0x00 in most platforms). ![]() In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. Return a String determined by Type( val) according to Table 20.įrom the MDN page about the behaviour of the typeof operator: null // This stands since the beginning of JavaScript.If IsUnresolvableReference( val) is true, return " undefined". Let val be the result of evaluating UnaryExpression.Ī.The production UnaryExpression : typeof UnaryExpression is evaluated as follows: If null is a primitive, why does typeof(null) return "object"?īecause the spec says so. If null is a primitive, why does typeof(null) return "object"? Explanation-1
0 Comments
Leave a Reply. |