Difference between null and undefined
In JavaScript, null and undefined are primitive data types used to indicate the absence of a value, but they have different meanings and use cases.
- undefined — means something is not defined. For example, a variable is created but nothing is assigned to it:
bash
let x; // x is undefined because we didn't assign a value
console.log(x); // undefined- null — means you explicitly stated that there's nothing here. For example:
bash
let y = null; // y is "nothing" because we decided so
console.log(y); // nullShort Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.