null vs undefined in JavaScript — What's the Difference?

undefined means a variable was declared but never assigned; null is an explicit 'no value' assigned intentionally. Learn how they compare and when each appears.

Published September 16, 2026

undefined is the default value JavaScript gives to a variable that has been declared but not assigned, or to a missing function argument or object property. null is a value a developer assigns deliberately to represent 'intentionally no value'.

Common causes

  • JavaScript automatically produces undefined in many situations: uninitialized variables, missing object properties, functions with no return statement
  • null never appears automatically — it only shows up where code explicitly sets a value to null

How to fix it

  • Use === (strict equality) when checking for either, since == treats null and undefined as equal to each other but not to any other value
  • Use the nullish coalescing operator (??) to provide a default only when a value is null or undefined, without affecting other falsy values like 0 or ''
  • Prefer null for values you explicitly want to represent as empty (e.g. resetting a selected item), and let undefined represent 'not set yet'

Example

typeof undefined // 'undefined'
typeof null      // 'object' (a long-standing JS quirk)
null == undefined  // true
null === undefined // false

FAQ

Why does typeof null return 'object'?

It's a bug from the very first JavaScript implementation that has been kept for backward compatibility ever since.

More JavaScript articles