Glossary / typeof

typeof

typeof is a unary operator that returns a string describing the broad type category of its operand. It is useful for primitive checks and has the unusual property that typeof someUndeclaredName does not throw. It is not a complete runtime type system: most objects collapse to "object", and historical quirks make a few results surprising.

Results and reliable checks

Common results include "undefined", "boolean", "number", "bigint", "string", "symbol", and "function". Arrays, dates, regular expressions, null, and ordinary objects generally report "object".

typeof null;                 // "object" (historic bug)
typeof (() => {});           // "function"
typeof undeclaredIdentifier; // "undefined"; does not resolve the binding

Use the check that matches the question: value === null for null, Array.isArray(value) for arrays (including cross-realm arrays), typeof value === "function" for callables in ordinary cases, and instanceof only when prototype-chain/realm assumptions are valid. For API payloads, validate the shape and constraints; typeof value === "object" also accepts null and does not say which properties exist.

The safe undeclared-name behavior does not make all access safe: typeof lexicalName still throws if lexicalName is in its temporal dead zone, and typeof undeclaredName.property throws while evaluating the property access.

Interview questions

  1. What is typeof null? "object", a legacy compatibility quirk.
  2. How detect an array? Array.isArray, not typeof or instanceof alone.
  3. Why does typeof undeclaredName not throw? It has a special rule for an unresolved identifier reference.
  4. Does typeof prove an object has a method? No; check or validate the property and its callability.

References