liveBook · Manning

Identifier resolution

As explained in Secrets of the JavaScript Ninja, Second Edition

The process of determining which binding a given identifier refers to in a particular program context.

When JavaScript encounters an identifier, it must determine which variable binding that name refers to. The running execution context performs this lookup through its lexical environment. section 5.3

Lookup follows the nested structure of the code:

  • Check the current lexical environment for the identifier. section 5.4.2
  • If it is not there, check outer environments in turn until a binding is found. section 5.4.2

Following the lookup

In the nested report and skulk example, intro is found in report’s environment, action in the outer skulk environment, and ninja in the global environment. section 5.4.2

Figure 5.9. How JavaScript engines resolve the values of variables

Trace how variable lookup moves through lexical environments to find a matching binding. section 5.4.2

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Traces lookup from the current lexical environment through outer environments, then turns to variable declaration forms.
Examples
Uses nested report and skulk functions to trace intro, action, and ninja to local, outer, and global environments.
What it assumes
Assumes familiarity with JavaScript function calls and execution contexts while explaining lexical-environment lookup.
What you can do after
Understand how JavaScript finds the binding for an identifier across nested lexical environments.
How it connects
Connects identifier resolution to execution contexts, lexical environments, and global environments.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.