The Differences Between null, undefined, and undeclared in JavaScript
JavaScript has three distinct ways to represent missing values. Understanding their differences is important for debugging and writing reliable code.
null can be understood as “nothing,” representing an empty value for a variable.
let emptyValue = null; // explicitly set to empty
undefined can be understood as “not yet,” indicating that a value has not been defined. For example, if you declare a variable but haven’t assigned a value to it, you’ll get undefined.
let unassignedValue; // value is undefined
console.log(unassignedValue); // undefined
undeclared can be understood as “never existed,” referring to something that has never been declared. When you try to use a variable that hasn’t been declared, you’ll get a ReferenceError.
console.log(nonExistentVariable); // throws ReferenceError: nonExistentVariable is not defined
Reference
What is the difference between null, undefined and undeclared in JavaScript?| ExplainThis