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
undefinedmeans a value was never assigned: an uninitialised variable, a missing object property, a missing function argument, or a function with noreturn.nullis 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; // falseUse ?? (nullish coalescing) to give a default for both: const name = input ?? 'Guest'.
Can you explain this in your own words?