2026-02-19


The Core Engine

In JavaScript, what you see on the screen is only 10% of the story. The other 90% happens in a silent, perfectly timed dance of memory and execution.


1. Scope — The "Who Can See What" Rule

Scope is the accessibility of variables. Think of it like security clearance in a building:

Why it matters: Understanding scope preents Variable Pollution—where different parts of your app accidentally overwrite each other's data.


2. Closures — The Function with a Memory

A closure is essentially a function that remembers its environment.

Imagine a photographer taking a picture of a room. Even if the room is demolished later, the photographer still has the photo. In JS, when a function is defined inside another, it closes over the variables of its parent.

Senior Use Case: Private Variables

Closures let us create variables that can only be changed through specific methods, hidden from the rest of the app.

function makeCounter() {
  let count = 0; // Private variable
  return () => ++count;
}

const counter = makeCounter();
counter(); // 1
counter(); // 2