👋 Hi! I'm Dastan, author of Hack Frontend. Always open to professional networking => connect with me on LinkedIn.
null vs undefined in JavaScript: Key Differences
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:
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:
let y = null; // y is "nothing" because we decided so
console.log(y); // null