typeof Checks and the null Quirk
Also Known As
typeof null
type checking
typeof operator
TL;DR
typeof returns a string describing the type — but typeof null === 'object' is a famous historical bug that cannot be fixed without breaking the web, requiring explicit null checks alongside typeof.
Explanation
The typeof operator returns one of: 'undefined', 'boolean', 'number', 'bigint', 'string', 'symbol', 'function', or 'object'. The bug: typeof null === 'object', which is wrong — null is not an object. This was a bug in the original JavaScript that became load-bearing and cannot be fixed. To check for null specifically, use value === null. To check for an object that is not null, use typeof value === 'object' && value !== null. TypeScript's type narrowing handles this automatically. PHP's gettype() behaves more sensibly: gettype(null) returns NULL.
Common Misconception
✗ typeof is sufficient for all type checks — typeof null returns 'object' (a known bug), arrays also return 'object', and you need instanceof or Array.isArray() for those cases.
Why It Matters
Incorrect typeof checks cause 'cannot read property of null' errors — understanding the null quirk and how to properly check types prevents an entire class of runtime bugs.
Common Mistakes
- Using typeof x === 'object' without checking x !== null
- Not using Array.isArray() for array checks — typeof [] === 'object'
- Using typeof for null checks instead of === null
Code Examples
✗ Vulnerable
// Unreliable type checks:
if (x == null) { } // Catches both null and undefined
typeof null === 'object' // Bug — null is not an object
NaN === NaN // false — can't use ===
✓ Fixed
// Reliable checks:
if (x === null) { } // Only null
if (x === undefined) { } // Only undefined
if (x == null) { } // null OR undefined (intentional)
Number.isNaN(x) // Correct NaN check
Array.isArray(x) // Correct array check
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
22 Mar 2026
Edited
23 Mar 2026
Views
20
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 6
Perplexity 3
Unknown AI 3
Google 2
Majestic 1
Ahrefs 1
How they use it
crawler 14
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Low
⚡ Quick Fix
For null checks always use === null; for arrays use Array.isArray(); for non-null object use typeof x === 'object' && x !== null — or switch to TypeScript
📦 Applies To
javascript ES5
web
cli
🔗 Prerequisites
🔍 Detection Hints
typeof x === 'object' without null check; typeof x to check for array; no Array.isArray() for array validation
Auto-detectable:
✓ Yes
eslint
typescript
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Medium
✓ Auto-fixable
Fix: Low
Context: Line