JavaScript Scope and Hoisting
Understanding scope hierarchy in JavaScript: Global, Module, Function, Block scope, lexical scoping, and variable/function hoisting.
JavaScript Scope and Hoisting
Scope determines the accessibility and visibility of variables, functions, and objects in some particular part of your code during runtime.
1. The 4 Scope Levels in JavaScript
[ Global Scope ] ──────────────────────────────────────────────┐
│ │
├─► [ Module Scope ] (ES Modules: import/export) │
│ │ │
│ ├─► [ Function Scope ] (Variables inside function) │
│ │ │ │
│ │ └─► [ Block Scope ] (Variables inside if/for {}) │1. Global Scope
Variables declared outside any function or block are in the global scope (attached to window in browsers or globalThis).
2. Function Scope
Variables declared inside a function are local to that function and inaccessible from outside.
3. Block Scope (let and const)
Variables declared with let and const inside curly braces {} cannot be accessed outside the block:
{
const blockSecret = "secret123";
let counter = 10;
var functionScoped = "leaked";
}
// console.log(blockSecret); // ReferenceError: blockSecret is not defined
console.log(functionScoped); // "leaked" (var does not respect block scope!)2. Lexical Scope (Scope Chain)
JavaScript uses lexical scoping (static scoping): an inner function has access to variables defined in its parent outer lexical environment:
const globalVar = "Global";
function outer() {
const outerVar = "Outer";
function inner() {
const innerVar = "Inner";
console.log(`${innerVar} -> ${outerVar} -> ${globalVar}`);
}
inner();
}
outer(); // "Inner -> Outer -> Global"3. Hoisting and Temporal Dead Zone
- Function Declarations: Hoisted completely with their body implementation. Can be called before their line of definition.
varDeclarations: Hoisted and initialized withundefined.letandconst: Hoisted into the Temporal Dead Zone (TDZ) without initialization. Accessing them before declaration throws aReferenceError.
Related Topics
JavaScript Objects and Property Access
Mastering JavaScript Objects: object literals, dot vs bracket notation, computed property names, Object.keys/values/entries, and shallow vs deep cloning.
JavaScript Closures and Encapsulation
Understanding closures in JavaScript: lexical environments, private state encapsulation, factory functions, and common loop closure pitfalls.