AceDevHub
Beginner JavaScript Interview QuestionsBeginnerConcept

JavaScript · Question 10

How does the typeof operator work in JavaScript, and what are its common quirks?

Direct answer

typeof returns a string describing the operand's type, but notable cases include typeof null === 'object' and typeof a function === 'function'.

The typeof operator is useful for quick runtime checks. Typical results include 'string', 'number', 'boolean', 'undefined', 'bigint', 'symbol', 'function', and 'object'.

  • typeof null returns 'object' because of a long-standing historical behavior.
  • typeof [] returns 'object'; use Array.isArray(value) when you specifically need to detect arrays.
  • typeof NaN returns 'number' even though NaN represents an invalid numeric result.