let/const actually enforce block scope, whereas var hoists to the top of the function. If you take a look at transpiled code meant for compatibility, it becomes clearer (I'm doing this by hand for readability):
I could have it slightly wrong, but back when they introduced the new keywords, this is how I remember it being explained to me. Tbh it was this that got me to switch over from CoffeeScript.
EDIT[1]: I had it wrong, like I thought. The code is better edited to reflect it.
EDIT[0]: fixed the function name, and also an article from last year that explains it:
View all comments
Show parent comment
85
u/greg0714 Jan 01 '22 edited Jan 01 '22
It's because of how the JS interpreter works..
Edit: From the creator of JS, Brendan Eich, on Twitter in 2014: