JavaScript - Hoisting
Overview
Estimated time: 15–20 minutes
Hoisting moves declarations to the top of their scope. var
/function are hoisted differently than let
/const
.
Learning Objectives
- Explain hoisting behavior for
var
and function declarations. - Recognize the temporal dead zone (TDZ) for
let
/const
.
Prerequisites
Examples
console.log(a); // undefined (declaration hoisted)
var a = 10;
// console.log(b); // ReferenceError (TDZ)
let b = 20;
foo(); // works (function declaration hoisted)
function foo(){ console.log('ok'); }
Notes
- Function declarations are hoisted with their bodies.
let
/const
are hoisted but uninitialized until declaration (TDZ).