JavaScript Scope & Hoisting
Scope determines where variables can be accessed, while hoisting moves declarations to the top. Master these fundamental concepts to understand how JavaScript executes your code.
What is scope and hoisting in JavaScript?
Scope decides where a variable can be used: global, function, or block scope (inside { } for let and const). Hoisting means declarations are processed before the code runs. Function declarations can be called before they appear, var variables exist early as undefined, and let/const exist but throw an error until their line runs (the temporal dead zone).
sayHi(); // works: function declarations are hoisted
function sayHi() { console.log("hi"); }
console.log(a); // undefined (var is hoisted)
var a = 1;
console.log(b); // ReferenceError (temporal dead zone)
let b = 2;What is Scope?
Scope determines where variables can be accessed in your code. It's like the "visibility rules" that JavaScript uses to determine which variables a particular piece of code can see and use.
Global Scope
Variables declared outside any function or block
var global = "Everyone can see me";Function Scope
Variables declared inside a function
function() { var local = 'Private'; }Block Scope
Variables declared inside { } with let/const
{ let block = 'ES6 only'; }What is Hoisting?
Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their scope during compilation. This means you can use variables and functions before they're declared in your code.
How Hoisting Works
What You Write
console.log(myVar); // undefined
var myVar = "Hello";How JavaScript Sees It
var myVar; // undefined
console.log(myVar); // undefined
myVar = "Hello";Hoisting Rules
var
Hoisted and initialized with undefined
let/const
Hoisted but in "temporal dead zone"
Functions
Fully hoisted (declaration + implementation)
Essential Examples
Code Editor
<pre id="out"></pre>Preview
Key Learning Points:
- • Global vs function vs block scope differences
- • Variable hoisting with var vs let/const behavior
- • Function hoisting and temporal dead zone
- • Common closure gotcha with loops
- • Essential patterns for modern JavaScript
Scope rules — step by step
Scope chain lookup
When you reference a variable, JS looks in the current scope first. If not found, it walks up the scope chain to the enclosing scope, then the next, until global. If not found anywhere, ReferenceError.
🎯 Quick Check
What does accessing a let variable before its declaration line throw?
Scope and Hoisting: summary
Key points
- Inner scopes can read outer variables, but not the other way round.
- A variable declared inside a function isn't visible outside it.
- Shadowing: an inner variable with the same name hides the outer one inside its block.
- Declare variables at the top of their scope with
let/constto avoid hoisting surprises.
Frequently asked questions
›What is the temporal dead zone?
The period between the start of a block and the line where a let or const is declared. Accessing the variable in that period throws a ReferenceError.