typeof null in JavaScript: A 2026 UK Guide

17 August 2026

Learn why typeof null returns 'object' in JavaScript, how to fix it, and best practices for null checks in 2026.

Understanding typeof null

In JavaScript, the `typeof` operator is meant to return the data type of a variable. However, when you run `typeof null`, it surprisingly returns `'object'`. This has been a well-known quirk for decades and often trips up developers, especially those new to the language. For UK developers working on modern Node.js services or front-end frameworks like React, understanding this behaviour is crucial. It can lead to subtle bugs if you rely on `typeof` to distinguish between objects and null. In 2026, with more complex data handling in UK tech projects, knowing exactly what `typeof null` returns is the first step to writing robust code.

The Historical Bug Behind It

Contrary to popular belief, `typeof null` returning `'object'` is not a deliberate design choice – it’s actually a bug from the very first version of JavaScript. In 1995, Brendan Eich implemented `typeof` with a type tag system, and `null` was assigned the tag `0`, which unfortunately corresponded to the object type. By the time this was discovered, fixing it would have broken existing code, so the bug was left in place. For UK devs, this story is a reminder that even the most widely used languages carry legacy quirks. When mentoring junior colleagues in UK coding bootcamps, explaining this history helps clarify why we can’t simply trust `typeof` for null checks.

How to Properly Check for Null

To check for null in JavaScript, you should never rely on `typeof null` because it gives a false positive for objects. Instead, use the strict equality operator: `value === null`. This will only return `true` when the value is exactly `null`. If you also need to handle `undefined`, use `value == null` (loose equality), which catches both. For UK developers building data validators or API handlers, this distinction is vital. For example, when receiving JSON payloads from a UK government API, you might need to check if an optional field is null. Using `value === null` ensures you don't accidentally treat `undefined` as null and vice versa.

Common Pitfalls in UK JavaScript Projects

In UK development teams, especially in sectors like fintech and healthcare where data precision is critical, the `typeof null` trap can cause real problems. A common pitfall is using `typeof value === 'object'` to filter out objects, which inadvertently includes null. This can lead to errors when iterating over arrays or accessing properties. Another pitfall is relying on `typeof` in Node.js server-side code to validate incoming data from UK postal code lookups or payment gateways. To avoid these issues, always pair `typeof` with explicit null checks. For instance, `if (value !== null && typeof value === 'object')` gives you the correct result. Adopting this pattern across your codebase will save hours of debugging.

Best Practices and Alternatives

In 2026, modern JavaScript offers cleaner alternatives that sidestep the `typeof null` quirk entirely. For optional chaining, use `?.` to safely access nested properties, which naturally handles null. For default values, use the nullish coalescing operator `??` instead of `||` to avoid treating empty strings or 0 as fallbacks. When validating types, consider using `Array.isArray(value)` for arrays, and for plain objects you can check with `Object.prototype.toString.call(value) === '[object Object]'` while explicitly excluding null. UK-based engineering blogs and articles consistently recommend these patterns, and the wider community is moving towards stricter checks. Make it a habit to write explicit null checks alongside `typeof` to keep your code predictable and maintainable.

FAQ

It's a legacy bug from the first version of JavaScript. Null was assigned a type tag that matched the object type in the original implementation. Fixing it would have broken existing code, so it was never corrected. This quirk remains in all versions of ECMAScript.

Latest guides