What is scope?
Scope is the set of rules that determines where a variable can be accessed. When you declare a variable, it’s only visible within a certain region of your code. Try to use it outside that region and you’ll get an error.orderId exists inside processOrder and nowhere else. This is a good thing — it means variables don’t leak into places they shouldn’t be.
Python mental model: this is the same general idea as Python scope (LEGB: Local, Enclosing, Global, Built-in). The biggest difference you’ll feel in JavaScript is that
let/const also create block scope for if/for/while.Block scope
Variables declared withconst and let are block-scoped. They exist only within the nearest set of curly braces {}.
if, for, while, and function body creates a new block scope:
- JavaScript
- Python
if and for blocks don’t create their own scope — variables leak out. In JavaScript, they don’t.
Function scope
Functions create their own scope. Variables declared inside a function are invisible outside it.Global scope
Variables declared outside any function or block are in the global scope. They’re accessible everywhere.Scope chain
When JavaScript encounters a variable, it searches from the current scope outward:color is a different variable in a different scope. The inner-most scope wins. This is called variable shadowing.
Python mental model: same idea as name shadowing in nested scopes — a local variable named
color hides an outer color. JavaScript’s scope chain and Python’s LEGB rule are solving the same problem.Variable shadowing is legal but can be confusing. Avoid reusing the same variable name in nested scopes. ESLint can warn you about this with the
no-shadow rule.Common mistakes
Accessing a block-scoped variable outside its block
Accessing a block-scoped variable outside its block
Hoisting confusion with var
Hoisting confusion with var
What’s next?
You understand how scope works — variables live inside blocks and functions, and inner scopes can see outer scopes. This leads directly to one of JavaScript’s most useful features: closures.Closures
How functions remember their surrounding variables