← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

typeof Checks and the null Quirk

javascript ES5 Beginner

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

Added 22 Mar 2026
Edited 23 Mar 2026
Views 20
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 6 Perplexity 3 Unknown AI 3 Google 2 Majestic 1 Ahrefs 1
crawler 14 crawler_json 1 pre-tracking 1
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

✓ schema.org compliant