Block/Function/Module Scope and the TDZ
Understand block, function, and module scope; see TDZ in action and avoid leaking variables.
Block/Function/Module Scope and the TDZ is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Know where variables live and when they are usable.
- Block scope (let/const)
- Function scope (var)
- Module scope
- TDZ safety

Block scope demo
Block scope keeps variables local to a pair of braces.
// Block scope: let/const exist only inside the braces
let lives = 3;
if (true) {
const bonus = 5;
let level = 1;
console.log("Inside block:", bonus, level);
}
console.log("Lives:", lives);
// Uncommenting next line would fail (not in scope):
// console.log(bonus, level);

Function scope with var
var is function-scoped (not block-scoped). Prefer let/const in modern JS.
// Function scope: var is visible across the whole function
function demo() {
if (true) {
var x = 10; // function-scoped
}
console.log("x is visible here:", x); // 10
}
demo();

Module scope basics
Module scope: top-level bindings are local to the file (ES modules).
// Module scope: top-level variables are local to the module file
const appName = "CoddyKit";
console.log("App name:", appName);
// In a real project, this would be its own file/module.
// Other files must import to access appName.

Temporal Dead Zone
TDZ prevents accidental early reads. Declare first, then use.
// TDZ: using let/const before declaration throws ReferenceError
try {
// Uncommenting would trigger TDZ:
// console.log(mode); // ReferenceError
let mode = "dark";
console.log("Mode after declare:", mode);
} catch (e) {
console.log("Caught:", e.message);
}

Scope pitfalls
Pitfalls:
- Do not mix var with let/const in the same scope.
- Declare close to usage.
- Avoid reading values before the declaration line (TDZ).

TDZ quiz
Quick check: TDZ behavior.

Recap
Recap: let/const are block-scoped; var is function-scoped; modules have their own file scope; TDZ stops early access.

Frequently asked questions
Is the “Block/Function/Module Scope and the TDZ” lesson free?
Yes — the full text of “Block/Function/Module Scope and the TDZ” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Block/Function/Module Scope and the TDZ”?
Understand block, function, and module scope; see TDZ in action and avoid leaking variables. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Block/Function/Module Scope and the TDZ” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- let, const, and avoiding var
- Block/Function/Module Scope and the TDZ
- Naming, Readability, and Common Pitfalls