# Hoisting & the Temporal Dead Zone

> Understand why JavaScript sometimes lets you use functions before you write them, and why let/const throw errors if you try the same trick.

Subject: JavaScript | Lesson 13 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/hoisting
Previous: [Functions: Declarations, Expressions & Arrows](https://codewithanimation.in/javascript/functions-basics.md) | Next: [Scope Chain & Closures](https://codewithanimation.in/javascript/scope-and-closures.md)

## What is Hoisting?

When JavaScript compiles your code, it scans for variable and function declarations and 'hoists' (lifts) them to the top of their current scope before the code actually runs.

This is why you can call a `function` on line 1, even if it is not defined until line 50. The engine already knows about it!

## The var trap

Historically, JavaScript only had the `var` keyword. When `var` is hoisted, the declaration goes to the top, but the *initialization* stays where it is.

This means if you try to log a `var` before it is initialized, you won't get an error. You will just get `undefined`. This led to notoriously hard-to-find bugs.

## The Temporal Dead Zone (TDZ)

To fix the `var` trap, ES6 introduced `let` and `const`.

`let` and `const` *are* actually hoisted to the top of the block, but they are not initialized with `undefined`.

The zone from the top of the block down to the actual line of code where the variable is declared is called the Temporal Dead Zone (TDZ). If you try to access the variable while it is in the TDZ, JavaScript throws a `ReferenceError`.

## Common mistake: Function Expressions aren't fully hoisted

If you write `const myFunc = () => {}`, the variable `myFunc` is hoisted into the TDZ, but the function itself isn't available until that line of code runs. Only standard `function myFunc() {}` declarations are fully hoisted with their bodies ready to use.

## Tip: Best Practices

To avoid hoisting confusion completely, always declare your variables at the top of their scope, and always use `let` or `const` instead of `var`.

## Try it: The TDZ Visualizer

Toggle between a standard `var` declaration and a modern `let` declaration to see exactly how hoisting and the Temporal Dead Zone behave differently at runtime.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/hoisting

## Check yourself

### Question 1

What happens if you try to `console.log` a `var` variable before the line where it is initialized?

- A. It logs undefined
- B. It throws a ReferenceError
- C. It logs the initialized value anyway

Correct answer: A

Because `var` is hoisted and immediately initialized with `undefined`, accessing it early does not throw an error; it just yields `undefined`.

- Why not B: Incorrect. That is what happens with let and const.
- Why not C: Incorrect. Only the declaration is hoisted, not the initialization value.

### Question 2

What is the Temporal Dead Zone (TDZ)?

- A. The space between the start of a scope and the line where a let/const variable is declared.
- B. The period of time after a function finishes executing.
- C. A memory leak caused by global variables.

Correct answer: A

The TDZ is the period of execution where a `let` or `const` variable exists in memory (it has been hoisted) but cannot be accessed yet because it hasn't been initialized.

- Why not B: Incorrect. The TDZ refers to variable access before declaration.
- Why not C: Incorrect. The TDZ is a specific behavior of let and const hoisting.

### Question 3

Which of the following is fully hoisted, meaning you can call it before it is defined?

- A. function sayHi() {}
- B. const sayHi = () => {}
- C. var sayHi = function() {}

Correct answer: A

Standard Function Declarations are fully hoisted (both the name and the body), allowing them to be invoked anywhere in their containing scope.

- Why not B: Incorrect. The variable `sayHi` is hoisted into the TDZ, so it cannot be used.
- Why not C: Incorrect. `var sayHi` is hoisted and initialized as `undefined`, so calling `sayHi()` throws a TypeError.

## Remember this

- Hoisting moves declarations to the top of the scope before execution.
- Function declarations are fully hoisted.
- `var` is hoisted and initialized to `undefined`.
- `let` and `const` are hoisted but uninitialized, creating a Temporal Dead Zone (TDZ) that throws a ReferenceError if accessed early.
