# let and const hoisting

**URL:** <https://es.discourse.group/t/let-and-const-hoisting/1093>\
**Category:** Spec Reading\
**Created:** [November 17, 2021, 5:03am UTC](https://es.discourse.group/t/let-and-const-hoisting/1093 "2021-11-17T05:03:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![1055610928](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/1055610928/32/1116_2.png) [@1055610928](https://es.discourse.group/u/1055610928)\
**Post date:** [November 17, 2021, 5:03am UTC](https://es.discourse.group/t/let-and-const-hoisting/1093/1 "2021-11-17T05:03:26Z")

</div>

Is there variable hoisting for let or const in JavaScript? Is it to put the variable in the TDZ area?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [November 17, 2021, 5:11am UTC](https://es.discourse.group/t/let-and-const-hoisting/1093/2 "2021-11-17T05:11:13Z")

</div>

The way I choose to think about it is the following:

Variables have three "stages": declaration (it exists in the scope), initialization (you can reference it, and it has the value `undefined`), assignment (it has whatever value the code assigns to it).

`var x = 3` hoists the declaration and initialization to the top of the current function's scope, and leaves the assignment (`x = 3`) where it is.

`let` and `const` hoist the declaration to the top of the current block's scope, but leave both the initialization and the assignment where it is.

The TDZ is the gap between declaration and initialization.

To be specific, if `const` and `let` did not hoist the declaration at all, then the following code would log 3 - if it hoisted the declaration and the initialization, there'd be no TDZ, and it would log 2. _Because_ it hoists the declaration but not the initialization, it throws a reference error.

```javascript
const a = 3; { console.log(a); const a = 2; }

```

(I'm sure many people may have a different mental model, or may disagree with this one; this is the one I hold and how I explain the concepts to newcomers).

---

<div class="post-metadata">

**Author:** ![1055610928](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/1055610928/32/1116_2.png) [@1055610928](https://es.discourse.group/u/1055610928)\
**Post date:** [November 17, 2021, 6:22am UTC](https://es.discourse.group/t/let-and-const-hoisting/1093/3 "2021-11-17T06:22:53Z")

</div>

Thank you for your answer. With your help, I understand
