Hoisting
Explains hoisting for declarations and how temporal dead zone affects let/const.
Short explanation
Hoisting is the behavior where declarations (variables and functions) are moved
to the top of their containing scope during compilation. For var, this means a
variable is available (as undefined) before its declaration; let/const are
hoisted but cannot be accessed until initialization (temporal dead zone).
Syntax example
console.log(x); // undefined (var hoisted)
var x = 5;
console.log(y); // ReferenceError (TDZ)
let y = 10;
How JS handles it internally
During the creation phase of execution contexts, variable and function
declarations populate the environment record. For var and function
declarations, bindings are initialized; for let/const, bindings exist but
are uninitialized until assignment.
FAQ
Q: Should I rely on hoisting?
A: It's clearer to declare variables before use; avoid relying on hoisting to prevent confusing code.