Hoisting and Temporal Dead Zone (TDZ)
Topic 7JavaScript Part 1
Hoisting and Temporal Dead Zone (TDZ)
Introduction
Hoisting refers to the process where variable and function declarations are moved to the top of their scope during the creation phase. The Temporal Dead Zone (TDZ) is the period between entering a scope and declaring a variable with let or const.
Learning Objectives
- •Learn how hoisting affects variables and functions.
- •Understand the concept of Temporal Dead Zone (TDZ) with let and const.
Key Concepts and Definitions
Hoisting
Variables declared with var are hoisted and initialized with undefined. Functions are hoisted with their full definitions.
TDZ
Variables declared with let or const exist in the scope but cannot be accessed before declaration.
Examples
Hoisting:
console.log(a); // undefined
var a = 10;
console.log(b); // ReferenceError (TDZ)
let b = 20;TDZ:
{
console.log(x); // ReferenceError
let x = 5;
}Practice Exercises
- •Identify the TDZ in code with let and const.
- •Write examples to demonstrate function and variable hoisting.
Interview guide discussion
(0)Join the discussion — login to comment and help other developers.
Login to comment