Skip to content

Lesson 4 of 9 · 25m

Execution Contexts, Environments, and Scope

What happens when JavaScript enters a function: bindings, environment records, hoisting, and why scope is lexical rather than dynamic.

JavaScript scope is not a runtime lookup through whatever objects happen to be around. It is lexical: the meaning of a variable is determined by where the code is written. Internally, every execution context carries an environment record — a specification object that holds bindings — and an outer pointer that forms the scope chain.

You do not need to implement the spec objects to use the model. You need enough of it to predict initialization errors and closure capture.

Three phases you should picture

When a function is entered, JavaScript creates its execution context, instantiates declarations, then executes statements:

function demo(input) {
  console.log(kind);   // undefined: var declaration was instantiated, assignment not run
  console.log(total);  // ReferenceError: total is in the temporal dead zone

  var kind = "demo";
  let total = input * 2;
}

var bindings are created and initialized to undefined for the function scope. let and const bindings are created but uninitialized until execution reaches their declaration — the temporal dead zone. Function declarations are initialized early enough to be called before their textual position in the same scope.

Scope is static, values are runtime

function outer() {
  const value = 10;

  function inner() {
    return value;
  }

  return inner;
}

inner does not copy value at return time. It keeps a reference to the environment in which it was created. This distinction matters: closures capture bindings, not immutable snapshots of their values.

Blocks are scopes too

{
  let token = "temporary";
}
console.log(token); // ReferenceError

let, const, class, and in strict modern code function declarations create block-level bindings. var ignores blocks and binds to the nearest function or global scope. This is one reason legacy code that mixes patterns is dangerous: the textual block suggests isolation the runtime does not provide.

Parameter scope has its own edge cases

Default parameter expressions are evaluated in a parameter scope. They can refer to earlier parameters but not to function-body let bindings:

function resize(width, height = width * 0.75) {
  return { width, height };
}

That separation is invisible until destructuring, defaults, and shadowing collide. If defaults become clever, extract a named value inside the function instead — future maintainers will thank you.

Field notes

  • typeof before initialization still throws for a lexical binding in its TDZ; the old typeof x safety trick applies to undeclared variables, not declared let bindings.
  • Shadowing is legal but expensive in human terms. Linters such as no-shadow catch real bugs in nested callbacks.
  • eval and legacy with distort scope reasoning. In modern codebases, banning them is not style; it is compiler hygiene.
  • Modules are strict mode and have their own top-level scope. Top-level this in an ES module is undefined, not window.

Was this lesson helpful?

Sign in to rate