ECMAScript 6 introduced the let
statement. I've heard it described as a "local" variable, but I'm still not quite sure how it behaves differently than the var
keyword.
What are the differences? When should let
be used over var
?
ECMAScript 6 introduced the let
statement. I've heard it described as a "local" variable, but I'm still not quite sure how it behaves differently than the var
keyword.
What are the differences? When should let
be used over var
?
Here's an explanation of the
let
keyword with some examples.This table on Wikipedia shows which browsers support Javascript 1.7.
Note that only Mozilla and Chrome browsers support it. IE, Safari, and potentially others don't.
var
is global scope (hoist-able) variable.let
andconst
is block scope.The difference is in the scope of the variables declared with each.
In practice, there are a number of useful consequences of the difference in scope:
let
variables are only visible in their nearest enclosing block ({ ... }
).let
variables are only usable in lines of code that occur after the variable is declared (even though they are hoisted!).let
variables may not be redeclared by a subsequentvar
orlet
.let
variables are not added to the globalwindow
object.let
variables are easy to use with closures (they do not cause race conditions).The restrictions imposed by
let
reduce the visibility of the variables and increase the likelihood that unexpected name collisions will be found early. This makes it easier to track and reason about variables, including their reachability(helping with reclaiming unused memory).Consequently,
let
variables are less likely to cause problems when used in large programs or when independently-developed frameworks are combined in new and unexpected ways.var
may still be useful if you are sure you want the single-binding effect when using a closure in a loop (#5) or for declaring externally-visible global variables in your code (#4). Use ofvar
for exports may be supplanted ifexport
migrates out of transpiler space and into the core language.Examples
1. No use outside nearest enclosing block: This block of code will throw a reference error because the second use of
x
occurs outside of the block where it is declared withlet
:In contrast, the same example with
var
works.2. No use before declaration:
This block of code will throw a
ReferenceError
before the code can be run becausex
is used before it is declared:In contrast, the same example with
var
parses and runs without throwing any exceptions.3. No redeclaration: The following code demonstrates that a variable declared with
let
may not be redeclared later:4. Globals not attached to
window
:5. Easy use with closures: Variables declared with
var
do not work well with closures inside loops. Here is a simple loop that outputs the sequence of values that the variablei
has at different points in time:Specifically, this outputs:
In JavaScript we often use variables at a significantly later time than when they are created. When we demonstrate this by delaying the output with a closure passed to
setTimeout
:... the output remains unchanged as long as we stick with
let
. In contrast, if we had usedvar i
instead:... the loop unexpectedly outputs "i is 5" five times:
May the following two functions show the difference:
Check this link in MDN
let
allows you to declare a variable that is limited in scope to the block (Local variable). The main difference is that the scope of a var variable is the entire enclosing function. For example, consider the following example, for usinglet
vs.var
: