Skip to the solution
EasyFrontend5 min read

What are the quirks of the typeof operator?

465 views

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 ReferenceError β€” typeof 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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 24 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is event bubbling and capturing?

Continue