Suggest an editImprove this articleRefine the answer for “Why typeof null is object”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`typeof null` returns the string `"object"`.** This is a historical JavaScript bug that dates back to the very first version of the language in 1995: values were stored as 32-bit tagged data where the first 3 bits encoded the type, and the object tag was `000`. The value `null` was all zeros (`0x00`), so the engine saw the object tag and answered `"object"`. The bug was never fixed for backward compatibility, so a `null` check must use a direct comparison. ```javascript typeof null; // "object" value === null; // the correct null check typeof value === 'object' && value !== null; // an object, but not null ``` **Key point:** `typeof null === "object"` is a bug frozen into the spec, so you cannot rely on `typeof` to detect `null`.Shown above the full answer for quick recall.Answer (EN)Image**`typeof null` returns `"object"`, and that is a historical JavaScript bug rather than a logical property of the language.** It has been there since 1995 and is kept deliberately so that millions of old scripts do not break. ## Theory ### TL;DR - `typeof null === "object"`, that is the result and nobody is going to change it. - The cause is the early 32-bit value representation where the object tag was `000` and `null` was all zeros. - It is an officially acknowledged "known bug", documented in ECMAScript. - It was never fixed for backward compatibility: a change would break an enormous amount of working code. - To check for `null`, use `value === null`, not `typeof`. - To tell a real object apart from `null`, you need a compound condition. ### Quick example ```javascript typeof null === "object"; // true ``` The result: `"object"`. ### The historical reason In early versions of JS, values were stored as **32-bit tagged data**: - the first **3 bits** encoded the **data type**; - the remaining **29 bits** held the value itself. Objects used the **type tag** `000`, that is three zeros at the start of the 32-bit value. And `null` was a value made **entirely of zeros** (`0x00`). So when evaluating `typeof null`, the engine saw the first three bits as `000` and concluded it was an object. That is how this came about: ```javascript typeof null; // "object" ``` ### Why it was never fixed Because millions of old scripts all over the internet may depend on this behaviour. Fixing it would cause mass breakage. > So the ECMAScript specification kept it as a **"known bug"** but **documented it officially**. ### How to check for null correctly Because of this bug you **cannot use `typeof` alone** to check for `null`. Wrong: ```javascript if (typeof value === 'object') { // value could easily be null here! } ``` Right: ```javascript if (value === null) { console.log('This is null'); } ``` Or, when you need to distinguish `null` from other objects: ```javascript if (typeof value === 'object' && value !== null) { console.log('This is an object, but not null'); } ``` ### An illustrative example ```javascript console.log(typeof null); // "object" console.log(typeof {}); // "object" console.log(typeof []); // "object" console.log(null === {}); // false console.log(null === undefined); // false ``` > All three (`null`, `{}`, `[]`) return `"object"`, yet they are **different data types**. ### Summary table | Check | Result | Explanation | | --- | --- | --- | | `typeof null` | `"object"` | Historical implementation bug | | `null === null` | `true` | The value equals itself | | `typeof undefined` | `"undefined"` | Expected behaviour | | `typeof {}` | `"object"` | A real object | | `typeof []` | `"object"` | An array is a subtype of object | ### Common mistakes - **Detecting an object with `typeof value === 'object'` and no extra condition.** `null` passes that check, and the next property access throws a `TypeError`. - **Believing that `null` is an object.** `null` is its own primitive type; only the `typeof` operator reports `"object"`. - **Confusing `null` and `undefined`.** `null === undefined` is `false`, although `null == undefined` is `true` because of type coercion. - **Hoping the bug will eventually be fixed.** It is frozen into the specification forever, so write code that accounts for it. - **Using `typeof value === 'null'`.** `typeof` never returns that string, so the condition is always false.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.