When learning JavaScript, one of the most important — and sometimes confusing — concepts you’ll encounter is scope and how it interacts with closures. These ideas determine how variables are accessed, preserved, and protected throughout your code. Understanding them not only helps you avoid errors but also unlocks powerful programming patterns for cleaner, more modular applications.

Let’s break it down step by step.

1. What is Scope?

Scope answers the question: “Where can I access a variable?”

JavaScript uses lexical (or static) scope, which means that the scope of a variable is determined by where you write the code, not where it is executed.