let: Block Scope and the Temporal Dead ZoneThe let keyword declares a reassignable, block-scoped variable introduced in ES6. Any variable declared with let exists only inside the { ... } block where you define it, such as an if branch or a for loop.
Before execution reaches a let declaration, the variable sits in the Temporal Dead Zone. Reading or writing it before its declaration line throws a ReferenceError instead of silently returning undefined.
flowchart TD
Enter["Enter Block Scope"] --> TDZ["Temporal Dead Zone: let count exists but cannot be read"]
TDZ -- "Access before declaration" --> Err["Throws ReferenceError"]
TDZ -- "Reach let count = 0" --> Init["Initialized and ready to read or reassign"]
Block scoping keeps temporary variables isolated inside if blocks and loops so outer variables never get overwritten by mistake.
let tax = 0.05;
if (true) {
// This 'tax' is a separate variable inside the if-block
let tax = 0.12;
console.log("Local block tax:", tax); // 0.12
}
console.log("Outer tax:", tax); // 0.05
// Each loop iteration gets its own fresh 'i' binding
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log("Iteration index:", i), 10);
}
let Fixes the Classic Loop Callback BugWith legacy var, a for loop shared one single i variable across every callback, so timers printed the final number three times. With let, JavaScript creates a fresh binding on every pass through the loop.
let score = 1; let score = 2; in the same block throws a SyntaxError. Reassign with score = 2; instead.let before its line: Moving console.log(name) above let name = "Kai" causes a ReferenceError due to the Temporal Dead Zone.let the exact same name as an outer variable makes debugging harder. Pick distinct names when possible.The Temporal Dead Zone is the span between entering a block scope and reaching the let or const initialization line. Accessing the variable inside that zone throws a ReferenceError.
let variable without assigning a value?Yes, let message; is valid and starts as undefined. Unlike const, you do not need to provide an initial value right away.
let attach properties to the global window object?No, declaring let appId = 5 at the top level of a script does not add window.appId, which keeps global scope cleaner than var.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.