Undefined and Null
Both undefined and null represent “no value”, but they are used in slightly different contexts.
-
undefinedmeans a variable has been declared but not assigned a value. Also the default return value of functions that don’t return anything.let x;console.log(x); // undefinedfunction foo() {}console.log(foo()); // undefinednull is an assignment value that represents "no object" or "empty". It must be explicitly set.
let y = null;console.log(y); // nullComparison:
console.log(undefined == null); // true (loose equality)console.log(undefined === null); // false (strict equality)In practice, use null when you want to explicitly indicate that a variable has no value, and let undefined represent unintentional absence.