4 ms·
> Unlike var, let and const statements are not hoisted to the top of their enclosing scope. No, let is hoisted to the top of the enclosing scope [1] ("temporal
by pcwalton 11y ago
> Unlike var, let and const statements are not hoisted to the top of their enclosing scope.
No, let is hoisted to the top of the enclosing scope [1] ("temporal dead zone" notwithstanding). let, however, is not hoisted to the top of the enclosing function.
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- mercer 11y agoI'd say that makes you technically correct, but practically speaking, if using let before it's declared (assigned?), this temporal dead zone, results in an error, it's pretty much not hoisted in the way that most of us think of it. Or is there a use case that I'm not aware of where the hoisting is beneficial despite the temporal dead zone?
- pcwalton 11y agolet f = function() { ... g(); ... } let g = function() { ... f(); ... } f(); Works despite the temporal dead zone and depends on hoisting. This exact example is why hoisting was kept for let.
- mercer 11y agoInteresting. This kind of stuff makes me want to dive into the details of these kinds of choices, as I regularly wonder why languages designed are a certain way.