How does typeof bypass ReferenceError when supplying an undeclared variable identifier?

foo; // ReferenceError: foo is not defined typeof(foo); // undefined 

How does typeof bypass ReferenceError when providing an unknown variable identifier? Is it just a javascript interpreter "magic" or can it be explained in terms of the concepts of "earth-earth"?

+3
javascript
source share
1 answer

No, this cannot be explained in the concept of custom lands - this is β€œmagic” if you want.

EcmaScript uses a reference specification type to explain such cases. These links are used to describe the semantics of assignments, method calls , eval and many others. Typically, the GetValue algorithm is called to dereference them (for example, when evaluating the expression of an expression), and this throws a ReferenceError when the link is not resolvable.

typeof operator , by contrast, does not just do GetValue, but has a special case for handling these links without a link: / p>

  1. If Type(val) is a reference, then

    but. If IsUnresolvableReference(val) is true , return "undefined" .

+6
source share

All Articles