Skip to solution
easyFrontend

What are the quirks of the typeof operator?

465 views
01

Understand the problem

Question presented to candidate: "If a variable was genuinely never declared anywhere, does typeof someUndeclaredVar throw an error? What if you just wrote someUndeclaredVar directly, without typeof — same answer?"

What a strong answer should cover:

  • 📌 Interview term: the real, direct answer to the prompt — verified directly: typeof on a genuinely undeclared variable does NOT throw at all — it genuinely returns the string "undefined". A bare reference to that SAME undeclared variable, without typeof, genuinely DOES throw a real ReferenceErrortypeof is a real, deliberate special case, safely checking for a variable's existence without risking a crash.
  • 📌 Interview term: typeof null is "object" — a famous, real historical bug, preserved for backward compatibility — covered in more depth in this bank's own dedicated null-vs-undefined question.
  • 📌 Interview term: typeof on arrays and NaN — verified directly: typeof [] is genuinely "object", not a separate "array" category (Array.isArray() is the correct, dedicated check); typeof NaN is genuinely "number", since NaN is a real, valid (if invalid-representing) member of the Number type.
  • 📌 Interview term: functions are the one real exception — verified directly: typeof on a function (including a class, which is genuinely a function under the hood) returns "function" — the ONE value typeof can return that does not correspond to one of the七 formal ECMAScript language types directly, a real, deliberate special case for a callable value.
  • A precise answer names typeof genuinely returning a real result for every one of the primitive types plus "object"/"function"/"undefined"Symbol() genuinely returns "symbol", and a BigInt literal genuinely returns "bigint" — verified directly, confirming typeof stays accurate for every newer primitive type added to the language too.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering the prompt's own undeclared-variable scenario (typeof vs. bare reference) is the strong signal, since it is the single most useful, commonly-missed typeof quirk.

Code / implementation expected: Yes — the direct side-by-side typeof undeclaredVar vs. a bare undeclaredVar reference is the clearest, most convincing demonstration.

typeof
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below, including the real thrown error, was actually run in Node.

1. Why T

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: typeof on an undeclared variable genuinely never throws, while a bare reference to it genuinely does
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 24 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track