TechnicalEasy JavaScript

What is the difference between null and undefined?

Asked for Frontend Developer, Full-Stack Developer

Why they ask this: A quick fundamentals question that checks attention to detail.

Answer

  • undefined means a value was never assigned: an uninitialised variable, a missing object property, a missing function argument, or a function with no return.
  • null is an intentional "no value" that a developer assigns.
js
let a;              // undefined
const obj = {};
obj.missing;        // undefined
let b = null;       // explicitly empty

typeof undefined;   // 'undefined'
typeof null;        // 'object' (a historical bug)
null == undefined;  // true
null === undefined; // false

Use ?? (nullish coalescing) to give a default for both: const name = input ?? 'Guest'.

Can you explain this in your own words?

Back to question bank