Lexical Environment
A lexical environment is the specification's model of a scope: a map of names to values, plus a link to the outer environment. Closures, hoisting, and this resolution are defined in terms of these environments.
Detailed Explanation
When people say "scope", they usually mean the idea a programmer uses: which names are visible here. A lexical environment is the same idea written down the way the ECMAScript specification writes it. Each environment has two parts. An environment record holds the bindings created in that scope. An outer reference points at the environment that encloses it, or at null for the realm's global environment.
The engine creates environments as your program enters scopes. A module gets one. A function call gets one (a new one every call). A block with let, const, class, or a function declaration in strict mode gets one. A catch clause gets one for the error binding. A for (let i ...) loop gets a fresh environment per iteration so that i is not shared across callbacks.
Name lookup is a walk: look in the current record, then follow the outer link, and so on. That walk is why an inner function can read an outer const, and why it cannot see a sibling function's locals. The walk is fixed when the function is created, not when it is called. The function object stores a reference to the environment that was current at creation. That stored link is the closure.
There are a few specialised records. A declarative record holds let, const, var (at function level), parameters, and function names. An object record uses an actual object as the binding store: the global environment's object record is globalThis, which is why var x = 1 in a browser script becomes window.x. A function environment record also tracks this and new.target and, for arrows, a flag that says "use the outer this". A module environment record holds exports and a link to the module's namespace.
Hoisting is environment instantiation. When a function environment is created, the engine already knows every var and function declaration in that function. It creates those bindings immediately, sets var to undefined, and installs function declarations as complete functions. let and const bindings are created too, but they stay uninitialised until their line runs — the temporal dead zone.
You reach for this model when a closure question, a this question, or a "why is this undefined" question needs a precise answer. "It looks at where the function was written" is the short version. "The function keeps a reference to the lexical environment where it was created, and lookups walk that chain" is the accurate one.
What it costs you: nothing at runtime that you did not already pay. Environments are the implementation of scope. The mental cost is treating them as a separate feature. They are not. They are the feature underneath scope, closures, and hoisting.
Code Examples
const rate = 0.1; // binding in the outer environment
function withTax(amount) {
// each call creates a new function environment
// outer link points at the environment that holds `rate`
return amount * (1 + rate);
}
console.log(withTax(100)); // => 110function makeCounter() {
let n = 0; // lives in makeCounter's environment
return function next() {
// next stores a link to that environment, so `n` survives
n += 1;
return n;
};
}
const a = makeCounter();
const b = makeCounter(); // a different environment, a different `n`
console.log(a(), a()); // => 1 2
console.log(b()); // => 1const obj = {
label: "box",
// ordinary method: function environment gets `this` = obj on the call
regular() {
return this.label;
},
// arrow: no own `this`; lookup walks to the outer environment
arrow: () => this,
};
console.log(obj.regular()); // => boxCommon Gotchas
A new environment is created per call, not per function object
two calls to the same function do not share `let` locals. They *do* share closed-over bindings from the same outer call.
`for (let i ...)` is a special case
the spec creates a fresh environment per iteration, which is why closures inside the loop see the matching `i`.
Global `var` is an object-environment binding
it is a property of `globalThis` and can be deleted in some cases. Module `const` is not.
Arrows copy the outer `this` binding
they do not get a function environment record of their own for `this`. That is a spec fact, not a metaphor.