JavaScriptEasy
JavaScript Scope — Scope Chain, Lexical Scope & Block Scope
Scope determines where variables are accessible in your code. Think of scope as a series of nested containers - inner containers can see outward, but outer containers cannot see inward. JavaScript has three types of scope: global (everywhere), function (inside functions), and block (inside curly braces with let/const).
6 concepts·2 practice problems·Beginner level
Key Concepts
- •Global scope: Variables accessible from anywhere in the code
- •Function scope: Variables declared with var inside a function
- •Block scope: Variables declared with let/const inside {}
- •Inner scopes can access outer scope variables (scope chain)
- •Outer scopes cannot access inner scope variables
- •Variables are looked up through the scope chain until found
Topics Covered
Scope Basics: Global, Function & BlockWhere variables live and are accessibleLexical Scoping & Scope ChainScope is determined at write time, not runtimeClosuresFunctions that remember their scopeHoistingHow JS moves declarations to the topVariable Hoisting: var vs let vs constHow var, let, and const are hoisted differentlyTemporal Dead Zone (TDZ) ExplainedWhy you cannot access let/const before declaration
Practice Problems
Common Mistakes to Avoid
- ×Thinking all { } create scope (only with let/const)
- ×Assuming outer scopes can see inner scope variables
- ×Not understanding that var ignores block scope
- ×Creating accidental global variables by forgetting const/let/var
Interview Tips
- ✓Explain scope as nested containers or bubbles
- ✓Know the difference between function scope and block scope
- ✓Be able to trace scope chain lookup
- ✓Understand variable shadowing