Facebook Pixel
undefined vs null in JavaScript

Why does typeof null return object in JavaScript?

The answer

It is a historical bug from the original implementation. null was represented with a type tag of 0, which the typeof check interpreted as object. It cannot be fixed without breaking existing code, so it remains.

More FAQs in undefined vs null in JavaScript

undefined is the engine's default for variables and properties with no value. null is a value the developer assigns to intentionally mean 'no value.' typeof undefined is 'undefined'; typeof null is 'object' (a historical bug).

Number(undefined) is NaN, so undefined + 5 is NaN. Number(null) is 0, so null + 5 is 5. This difference is a real source of bugs in arithmetic.

JSON.stringify omits properties with undefined values, but preserves properties with null values as null. This matters for API payloads where the presence of a null key has meaning.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.