0

Why are typeof null and undefined objects instead of keywords or booleans in JavaScript?

For example,

(typeof null)
// "object"
(typeof undefined)
// "undefined"

But the thing is with undefined is that it acts like an object without properties. But typeof null is an object. Why are they objects and what is the point of them being objects without properties?

Parking Master
  • 551
  • 1
  • 4
  • 20
  • https://stackoverflow.com/questions/18808226/why-is-typeof-null-object – mrJQuery Nov 04 '21 at 15:52
  • Because [that's how the language was designed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#null_type) - in twelve days. – Andy Nov 04 '21 at 15:54
  • 2
    Neither `null` nor `undefined` is an object. `null` _is_ a keyword and a value of the Null type. `undefined` is not a keyword, but it is a global immutable property of `globalThis` and a value of the Undefined type. `typeof` is broken. The types are listed in the [specification](//tc39.es/ecma262/multipage/#sec-ecmascript-language-types). `typeof null` is also not an object; it’s a string. How did you arrive at the conclusion that `undefined` acts like an object without properties? – Sebastian Simon Nov 04 '21 at 15:57
  • Booleans are `true` and `false`; they are unrelated to `null` and `undefined`. (Oh, I see, it’s you, the author of that `Array.prototype.some` answer from yesterday.) – Sebastian Simon Nov 04 '21 at 16:02
  • Yes that's me, I actually ask understandable questions. – Parking Master Nov 04 '21 at 16:15

0 Answers0