Short answer:
typeof
has a special case for unresolvable references; it explicitly returns undefined
if the reference is unresolvable.
Long answer:
The typeof operator has a special case for unresolvable references:
11.4.3 The typeof Operator
The production UnaryExpression :
typeof
UnaryExpression is evaluated as follows:
- Let val be the result of evaluating UnaryExpression.
- If Type(val) is Reference, then
- If IsUnresolvableReference(val) is
true
, return "undefined
".- Let val be GetValue(val).
- Return a String determined by Type(val) according to Table 20.
On the other hand, the internal GetValue(V)
function, which is used everywhere in javascript, including for retrieving the value of a varialbe, throws a ReferenceError if the reference is unresolvable:
8.7.1 GetValue (V)
- If Type(V) is not Reference, return V.
- Let base be the result of calling GetBase(V).
- If IsUnresolvableReference(V), throw a
ReferenceError
exception.- If IsPropertyReference(V), then
[...]
See the spec.