# Hoisting & Temporal Dead Zone

> Explain hoisting in JavaScript. How do var, let, const, and function declarations behave differently?

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-hoisting-interview
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

Explain hoisting in JavaScript. How do var, let, const, and function declarations behave differently?

## Answer

Hoisting is JavaScript's default behavior of moving declarations to the top of their scope during the compilation phase. However, only declarations are hoisted, not initializations.

- var: hoisted and initialized with undefined
- let/const: hoisted but NOT initialized (Temporal Dead Zone)
- Function declarations: fully hoisted (both name and body)
- Function expressions: only the variable is hoisted (not the function body)

**Creation phase, execution phase, and the TDZ. Switch between var, let, const and function forms under Tweak.**

1. **Creation phase**: The engine registers every declaration in the scope before running code.
2. **var**: Initialised to undefined, so early reads return undefined.
3. **let / const**: Registered but uninitialised: reading before the declaration throws a ReferenceError (the temporal dead zone).
4. **Function declarations**: Hoisted with their body, so they can be called before their line.
5. **Function expressions**: Follow their variable's rules: var greet is undefined, so calling it throws TypeError.

## Code examples

### var Hoisting

```
console.log(x); // undefined (not ReferenceError)
var x = 5;
console.log(x); // 5

// The engine sees it as:
// var x;          // declaration hoisted
// console.log(x); // undefined
// x = 5;          // assignment stays
// console.log(x); // 5
```

var declarations are hoisted and initialized with undefined. The assignment stays in place.

### let/const and Temporal Dead Zone

```
// This throws ReferenceError
try {
  console.log(y);
  let y = 10;
} catch (e) {
  console.log(e.message);
}

// The TDZ exists from the start of the block
// until the declaration is encountered
{
  // TDZ starts here for 'z'
  // console.log(z); // would throw ReferenceError
  let z = 20; // TDZ ends here
  console.log(z); // 20
}
```

let and const are hoisted but remain in a 'Temporal Dead Zone' until the declaration line is reached.

### Function Declaration vs Expression

```
// Function declaration - fully hoisted
console.log(greet('Alice'));
function greet(name) {
  return `Hello, ${name}!`;
}

// Function expression - only variable hoisted
try {
  console.log(farewell('Bob'));
} catch (e) {
  console.log(e.message);
}
var farewell = function(name) {
  return `Bye, ${name}!`;
};
```

Function declarations are fully hoisted. Function expressions with var hoist the variable as undefined, causing a TypeError when called.

## Key points

- Hoisting happens during the creation phase of the execution context
- let and const ARE hoisted - they just can't be accessed before declaration (TDZ)
- Class declarations are also in the TDZ like let/const

## Common follow-up questions

- What exactly happens during the creation phase?
- Why was the TDZ introduced with let/const?
- What is the output of: console.log(typeof undeclaredVar)?

## Interview tips

- Many candidates say let/const are not hoisted - this is wrong. They ARE hoisted but in TDZ
- Always explain the WHY - TDZ prevents using variables before they're ready
- Draw a timeline showing declaration phase vs execution phase

## Concepts covered

Hoisting, TDZ, var, let, const, Function Declaration

Previous: [Closures & Lexical Scope](https://www.internhack.in/learn/interview/javascript-interview/js-closures-interview) · Next: [Object Methods & Property Descriptors](https://www.internhack.in/learn/interview/javascript-interview/js-object-methods) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [this Keyword & Binding Rules](https://www.internhack.in/learn/interview/javascript-interview/js-this-keyword)
- [call, apply, bind](https://www.internhack.in/learn/interview/javascript-interview/js-call-apply-bind)
- [Prototypes & Prototype Chain](https://www.internhack.in/learn/interview/javascript-interview/js-prototypes)
- [Classes & Inheritance](https://www.internhack.in/learn/interview/javascript-interview/js-classes-inheritance)
- [Promises Deep Dive](https://www.internhack.in/learn/interview/javascript-interview/js-promises-deep)
- [async/await Patterns](https://www.internhack.in/learn/interview/javascript-interview/js-async-await)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
