# Hoisting

> The Hoisting JS mechanism makes declarations available before the line where they appear in their scope.

## What is hoisting in JavaScript?

The mechanism where variable and function declarations are processed before the code runs, as if moved to the top of their scope. `var` is hoisted with the value `undefined`, function declarations are hoisted fully, while `let` and `const` stay in the temporal dead zone until their line runs.

- Topic: Basics
- Type: concept

## Example

Wrong:

```js
init();
const init = () => {
  console.log('ready');
};
```

Right:

```js
init();
function init() {
  console.log('ready');
}
```

Function declarations are hoisted with their body; a `const` arrow is in the temporal dead zone until its line.

## Common mistakes

- A `ReferenceError` from using a `let` variable in its temporal dead zone.
- Calling an arrow function stored in a `const` before the line that defines it.

## Key points

- No `let`, `const` or class is used before its declaration line.
- Function expressions and arrow functions are defined before they are called.
- Variables are declared with `const` or `let`, not `var`, to avoid silent `undefined` reads.

## Interview questions

### Are let and const hoisted?

Yes, but they are not initialized. Accessing them before the declaration throws a `ReferenceError` (the temporal dead zone).

### Why is a var variable undefined before its line?

`var` declarations are hoisted and initialized to `undefined`; only the assignment stays in place.

### Are classes hoisted?

Class declarations behave like `let`: they are hoisted but cannot be used before the declaration.

## Related cards

- [var](https://wonderium.dev/js/var.md)
- [let](https://wonderium.dev/js/let.md)
- [const](https://wonderium.dev/js/const.md)
- [function](https://wonderium.dev/js/function.md)
- [Execution Context](https://wonderium.dev/js/execution-context.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/hoisting
- Last updated: 2026-11-25
