Member-only story

Why is typeof null === “object”?

Beck Moulton
5 min readMay 9, 2024

--

The task of the unary typeof operator is to return a string representation of the operand type. In other words, a string will be returned, and all possible values of the type returned by the typeof operator are listed in the ECMA-262–13.5.1 specification. According to this idea, the value returned by the operator must correspond to the data type accepted in the same specification. However, upon closer inspection,

It can be noted that it should return, although this is a fairly independent type, it is described in Section 6.1.2. The reason for this is usually human factors, or simply, innocent errors in the code. How this error occurs,

Let’s analyze the browser source code in this article and try to figure it out. typeof 1 “number” typeof “” string “typeof null” object “Null

Mocha

Perhaps it is worth starting from the very beginning of JavaScript, the prototype language Mocha, created by Brendan Eich in just 10 days in 1995, later renamed LiveScript, and even later, in 1996, JavaScript became known to us today.

Unfortunately, the source code of Mocha has never been released, and we do not know its exact appearance in 1995. However, in a comment on an article on Dr. Alex Rauschmayer’s blog, Eich wrote that he used the “discrimination union” technique, which is also a “tag union”, where he used two fields. The struct structure may look like this, for example:

enum JSType {
OBJECT,
FUNCTION,
NUMBER,
STRING…

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet