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. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.
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.

Learn JavaScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 59
- Lessons
- 200
Frequently Asked Questions
Is the “Block/Function/Module Scope and the TDZ” lesson free?
Yes — any 3 lessons of the JavaScript Academy learning path, including “Block/Function/Module Scope and the TDZ”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
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