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]
The question, the details, the next step.
A practical checklist
Use
value === nullwhen you mean to test specifically fornull.Remember that
typeof nullreturns"object"for historical compatibility.Do not treat
typeof value === "object"as a specific null test.Compare the condition with a
nullvalue 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.
- [S1] typeof — JavaScript | MDN developer.mozilla.org · Checked
