Skip to main content

πŸ“„ Please explain the difference between null and undefined

Comparing the differences​

  • undefined:
    • Indicates that a variable has been declared but not yet assigned a value.
    • Is the default value of uninitialized variables.
    • Functions return undefined by default if no return value is explicitly specified.
  • null:
    • Represents an empty value or no value.
    • Usually must be explicitly assigned as null.
    • Used to indicate that a variable intentionally does not point to any object or value.

Examples​

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

Use typeof for verification​

console.log(typeof undefined); // Output: "undefined"
console.log(typeof null); // Output: "object"

console.log(null == undefined); // Output: true
console.log(null === undefined); // Output: false