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:
- Global Scope — The lobby. Everyone has access to the information here.
- Function Scope — A private office. People inside can see the lobby, but the lobby can't see your desk.
- Block Scope (
let/const) — A temporary locker. Once the block ends, the contents vanish.
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