MediumCore JS📖 Theory Question

What is an Execution Context and what are its two phases?

💡

Hint

Creation phase (hoisting, this binding) then execution phase — pushed onto the call stack

Full Answer

An Execution Context (EC) is the environment in which JavaScript code evaluates and executes. Every function call creates a new EC pushed onto the call stack.

Phase 1 — Creation Phase:

  • Scans for var declarations → hoisted and set to undefined
  • Scans for let/const → hoisted but placed in Temporal Dead Zone
  • Scans for function declarations → fully hoisted (name + body)
  • Determines the value of this
  • Sets up the scope chain (reference to outer environment)

Phase 2 — Execution Phase: runs code line by line, assigns actual values.

function example() {
  // Creation phase saw: var a → undefined, fn fully hoisted
  console.log(a);   // undefined (var hoisted)
  console.log(fn()); // 'works!' (function declaration hoisted)
  var a = 1;
  function fn() { return 'works!'; }
  console.log(a);   // 1 (now assigned)
}

Types of EC: Global EC (one per program), Function EC (one per call), Eval EC (avoid).

💡 The Global EC creates the global object (window/globalThis) and binds this = global object in its creation phase.

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