Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

So what's the point of having let, then?


let declares a block-scoped variable, while var declares a function-scoped variable.

Limiting a variable's scope can help avoid subtle and potentially annoying errors. For example, if you use a let variable inside an if block, it'll only be accessible by code inside the block. If you use var inside an if block, your variable will be visible to the entire function.


The truth is that if you need to declare a variable outside your current scope you should probably declare it in the outside scope in the first place.

In the scenario with var/let I need to grok the code in order to tell which variables are visible in my current scope.


I tend to mainly use const, let only when necessary. I never use var and set linter to scream about it at me. IE11 supports it, so unless you develop application for IE compatibility mode (my condolences) I don't see a reason to use var. On the other hand var mainly bites you when declaring closures inside loops, also hoisting is just plain weird.


I find hoisting a convenient feature as I can declare the variable in context to where it's used. It means I do not have to break the flow of how the code reads, making the code easier to understand and less bug prone. Example:

    if(something) var foo = 1;


So how do you maintain the invariant that this variable is used only if <something> is true _down the line_?


It's only logical that it will be undefined if it's never assigned. With var you can just declare anywhere. While with let it feels like a chore when you have to declare in a parent block, eg. outside the if-block for it to be accessible within another if-block. Lexical function scope works very well in an async language with first class functions, as you deal mostly with functions, which can access it's closures at any time, etc. makes it logical that the function should define the scope, not if-statement or for-loops.


let also does some magic in for-loops, creating a new variable for each iteration, basically creating a closure.

It also throws and error if it's used before it's declared.

Let basically fix some minor issues that hard-bitten JavaScript developers have learned to avoid.


block scope




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: