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:
typeofon a genuinely undeclared variable does NOT throw at all — it genuinely returns the string"undefined". A bare reference to that SAME undeclared variable, withouttypeof, genuinely DOES throw a realReferenceError—typeofis a real, deliberate special case, safely checking for a variable's existence without risking a crash. - 📌 Interview term:
typeof nullis"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:
typeofon arrays andNaN— verified directly:typeof []is genuinely"object", not a separate"array"category (Array.isArray()is the correct, dedicated check);typeof NaNis genuinely"number", sinceNaNis a real, valid (if invalid-representing) member of the Number type. - 📌 Interview term: functions are the one real exception — verified directly:
typeofon a function (including a class, which is genuinely a function under the hood) returns"function"— the ONE valuetypeofcan 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
typeofgenuinely 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, confirmingtypeofstays 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
typeofquirk.
Code / implementation expected: Yes — the direct side-by-side typeof undeclaredVar vs. a bare undeclaredVar reference is the clearest, most convincing demonstration.