🟡 MediumCore JS📖 Theory Question

What are Environment Records and how do they underpin scope?

💡

Hint

The data structure that stores variable bindings in each scope — the "real" scope implementation

Full Answer

An Environment Record is the actual data structure that stores identifier (variable) bindings for a scope. When the spec says "scope," it means Environment Records under the hood.

// Each scope = one Environment Record created at runtime
// Environment Record stores { variable: value } mappings

function outer() {
  let x = 1;     // x stored in outer's Environment Record
  function inner() {
    let y = 2;   // y stored in inner's Environment Record
    console.log(x); // looks up outer's Environment Record via [[OuterEnv]]
  }
  inner();
}
// Call stack at inner():
// inner's ER: { y: 2, [[OuterEnv]] → outer's ER }
// outer's ER: { x: 1, inner: fn, [[OuterEnv]] → global ER }
// global ER:  { outer: fn, ... }

// Types of Environment Records:
// - Declarative ER: let, const, function declarations, parameters
// - Object ER: var declarations and global code (backed by an object)
// - Global ER: combination of both (global scope)
// - Module ER: ES module scope
// - Function ER: function body scope

// Closures = an inner function holding a reference to
// the outer function's Environment Record AFTER it has returned
const fn = outer(); // outer's ER stays alive because inner references it
💡 Environment Records replaced the older "Activation Object" spec term. Understanding them demystifies closures completely: a closure is just a function holding a reference to an outer Environment Record.

More Core JS Questions

🟢 EasyWhat is the difference between var, let, and const?→🟢 EasyExplain closures with a practical example.→🟢 EasyWhat is hoisting in JavaScript?→🟢 EasyExplain the event loop, call stack, and microtask queue.→

Practice this in a timed sprint →

5 free questions, no signup required

âš¡ Start Sprint