[Easy] π null & undefined
Key Differencesβ
undefinedοΌ- Indicates a variable has been declared but not assigned.
- It is the default value for uninitialized variables.
- A function returns
undefinedif no explicit return value is provided.
nullοΌ- Represents an empty value or no value.
- Usually assigned explicitly as
null. - Used to indicate a variable intentionally points to nothing.
Exampleβ
let x;
console.log(x); // output: undefined
function foo() {}
console.log(foo()); // output: undefined
let y = null;
console.log(y); // output: null
let obj = { x: 5 };
obj.x = null;
console.log(obj.x); // output: null
Validation with typeofβ
console.log(typeof undefined); // output: "undefined"
console.log(typeof null); // output: "object"
console.log(null == undefined); // output: true
console.log(null === undefined); // output: false