Middle
What is the difference between null and undefined?
sobes.tech AI
Answer from AI
In JavaScript, null and undefined are different types that both denote the absence of a value but with different meanings:
-
undefinedmeans that a variable has been declared but not assigned a value. This is the default state for undeclared properties or variables. -
nullis an explicit assignment of a value, meaning "no value" or "empty value".
Example:
let a;
console.log(a); // undefined — the variable is declared but not initialized
let b = null;
console.log(b); // null — the variable is explicitly assigned an "empty" value
Thus, undefined is the default absence of a value, while null is an intentional indication of no value.