Sobes.tech
Middle

What are null and undefined, and what is the difference? What will typeof null return?

sobes.tech AI

Answer from AI

null and undefined are two different types in JavaScript that denote the absence of a value but are used in different situations:

  • undefined means that a variable is declared but not assigned a value.
  • null is an explicit assignment of the value "empty" or "no object".

Difference:

  • undefined is the default type for undeclared or uninitialized variables.
  • null is an object that a programmer can assign to a variable to indicate the absence of a value.

What does typeof null return?

console.log(typeof null); // "object"

This is a historical quirk of JavaScript: null is considered an object, although by its nature it is a primitive representing an empty value.