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.