Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does `typeof null` return `"object"` in JavaScript?

Quick answer

typeof null returns "object" because of a historical JavaScript compatibility behavior; it does not mean null is an object. To check whether a value is specifically null, use strict equality: value === null. For example, typeof null is "object", while null === null is true. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does typeof null say "object"? I expected it to say "null".

Geez

typeof null returns "object" for historical compatibility. In an early JavaScript implementation, values had type tags: objects used tag 0, and null was represented as a null pointer with that same tag. A proposal to change the result to "null" was rejected, so the behavior remains. It’s a historical quirk—not a reason to treat null as an object. [S1]

For a concrete comparison:

const value = null;

console.log(typeof value); // "object"
console.log(value === null); // true

The first expression asks for the typeof result. The second directly checks whether the value is null.

Follow-up question

Does that mean I should avoid typeof altogether?

Geez

No. The important point is to use a check that matches your question. If you need to know whether a value is specifically null, typeof does not give you a reliable null test: for null, its result is "object". Use strict equality instead:

if (value === null) {
  // Handle the case where value is null.
}

That condition expresses the specific check you want. It avoids interpreting the typeof result as though it named null.

Follow-up question

What can go wrong if I use typeof value === "object" as my check?

Geez

It matches the typeof result for null too, because typeof null is "object". So if your code intends to identify null specifically, that condition is not the right test. Compare the two expressions with a null value:

const value = null;

console.log(typeof value === "object"); // true
console.log(value === null); // true

Both comparisons are true here, but they answer different questions: the first checks the typeof result; the second checks whether the value is null. Choose the second when null is what you mean to detect. [S1]

Follow-up question

How should I remember which check to use when reading code?

Geez

Start with the condition’s intent. If it asks, “Is this value specifically null?”, look for value === null. Don’t infer that null is an object from the typeof result; that result is a historical compatibility behavior. [S1]

A compact example to keep the distinction clear:

const value = null;

console.log(typeof value); // "object"
console.log(value === null); // true

When reviewing a condition such as typeof value === "object", remember that it is also true for null. If the code needs to recognize null specifically, use the direct strict comparison instead.

A practical checklist

  • Use value === null when you mean to test specifically for null.

  • Remember that typeof null returns "object" for historical compatibility.

  • Do not treat typeof value === "object" as a specific null test.

  • Compare the condition with a null value when checking what it expresses.

A few more useful answers

Does `typeof null === "object"` mean null is an object?

No. It is a historical compatibility behavior of typeof. Use value === null to test whether a value is specifically null. [S1]

What should I use instead of `typeof value === "object"` to check for null?

Use value === null. That directly tests whether the value is null, rather than checking the result of typeof. [S1]

Sources

References used for this guide.

  1. [S1] typeof — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work