What is the difference between null and undefined in JavaScript?

JavaScript Fundamentals Basic Fresher1–2 Yrs Conceptual 1 min read

Quick Answer

undefined means a variable has been declared but not assigned, or a property/return value doesn't exist — it's the language's default “absence”. null is an explicit “no value” that you assign deliberately. typeof undefined is "undefined"; typeof null is "object" (a historic bug).

undefined is JavaScript's default for absence — a variable you declared but didn't assign, a missing argument, a property that isn't there, a function that doesn't return anything. null is a value you set on purpose to mean “intentionally empty”. A quirk to know: typeof null returns "object", which is a long-standing bug in the language. The nullish coalescing operator ?? and optional chaining ?. both treat null and undefined the same, which is usually what you want.

  • Unassigned variable, missing function argument, missing object property, function with no return → undefined.
  • You write user.manager = null to say “this user explicitly has no manager”.
  • null == undefined is true; null === undefined is false.
  • Nullish operators: a ?? b uses b only when a is null or undefined (not for 0/'' like ||). a?.b short-circuits to undefined if a is nullish.
  • JSON has null but not undefined — undefined properties are dropped on JSON.stringify.