Hacker News new | ask | show | jobs
by pcwalton 3804 days 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/Refe...

1 comments

I'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?

    let 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.
Interesting. 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.