OnlineCBT Logo OnlineCBT
JavaScript Tutorial
JAVASCRIPT · LESSON 10

Scope, Closures, and Execution Context

By Ratan Updated: 16 Oct 2026

Understand global/local scope, lexical scope, closure functions, and the call stack.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

A closure remembers bindings from its creation scope

JavaScript resolves names using lexical scope: where the function was written, not simply who called it. Each function call has its own local state. A returned function can retain access to that state after the outer call finishes.

function createCounter(start = 0) {
  let count = start;
  return {
    next() { count += 1; return count; },
    current() { return count; }
  };
}
const first = createCounter(10);
const second = createCounter();
console.log(first.next()); // 11
console.log(first.next()); // 12
console.log(second.next()); // 1
console.log(first.current()); // 12

The two outer calls create separate count bindings. The methods retain access to their own call's count. A closure remembers the binding, not a permanently frozen copy of its original value.

Execution context and call stack

Calling a function adds execution work to the call stack; returning removes that active call. Retained lexical data can still be reachable through a closure after the active call ends. This distinction explains why “the function returned” does not mean every object it referenced is immediately discarded.

Loop bindings

for (let index = 0; index < 3; index += 1) {
  setTimeout(() => console.log(index), 0);
}
// Later: 0, 1, 2

let provides per-iteration bindings here. A similar loop using var shares one binding and the callbacks usually observe the final value 3 after the loop completes. The timers do not run in the middle of the current synchronous loop.

Lifetime and accidental retention

A listener or timer can keep a closure reachable, which can in turn retain objects it needs. Remove obsolete listeners and cancel unneeded timers when a component is disposed. Do not call every closure a memory leak; retention becomes a leak when data remains reachable longer than the application requires.

Practice

  1. Add a reset method that restores a counter to its initial start.
  2. Create two counters and verify that resetting one does not reset the other.
  3. Predict a callback that reads a changed outer variable; it sees the current binding value when invoked.
  4. Trace the stack for a function calling another function, separately from the lexical scopes.

हिन्दी संकेत: closure value की पुरानी photocopy नहीं, outer binding तक access रखता है।

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat