Difference between null and undefined in JavaScript:
🔸 undefined
- A variable that has been declared but not assigned a value is
undefined. - Also, functions return
undefinedif no return value is specified. - It is the default value for uninitialized variables.
✅ Example:
let a;
console.log(a); // undefined
function test() {}
console.log(test()); // undefined
🔸 null
nullis an intentional absence of any object value.- It's manually assigned to a variable to indicate “no value”.
✅ Example:
let b = null;
console.log(b); // null
✅ Key Differences
undefinedmeans not assigned yet.nullmeans explicitly assigned as empty.- Type of
undefinedis"undefined". - Type of
nullis"object"(due to a historical bug).