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
?
ECMAScript 6 added one more keyword to declare variables other the "const" other than "let".
The primary goal of introduction of "let" and "const" over "var" is to have block scoping instead of traditional lexical scoping. This article explains very briefly difference between "var" and "let" and it also covers the discussion on "const".
Previously there were only two scopes in JavaScript, i.e. functional and global. With '
let
' keyword JavaScript has now introducedblock-level
variables.To have a complete understanding of the 'let' keyword, ES6: ‘let’ keyword to declare variable in JavaScript will help.
let
is interesting, because it allows us to do something like this:Which results in counting [0, 7].
Whereas
Only counts [0, 1].
let
can also be used to avoid problems with closures. It binds fresh value rather than keeping an old reference as shown in examples below.DEMO
Code above demonstrates a classic JavaScript closure problem. Reference to the
i
variable is being stored in the click handler closure, rather than the actual value ofi
.Every single click handler will refer to the same object because there’s only one counter object which holds 6 so you get six on each click.
General workaround is to wrap this in an anonymous function and pass
i
as argument. Such issues can also be avoided now by usinglet
insteadvar
as shown in code below.DEMO (Tested in Chrome and Firefox 50)
The difference is scoping.
var
is scoped to the nearest function block andlet
is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.Also, variables declared with
let
are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.Demo:
Global:
They are very similar when used like this outside a function block.
However, global variables defined with
let
will not be added as properties on the globalwindow
object like those defined withvar
.Function:
They are identical when used like this in a function block.
Block:
Here is the difference.
let
is only visible in thefor()
loop andvar
is visible to the whole function.Redeclaration:
Assuming strict mode,
var
will let you re-declare the same variable in the same scope. On the other hand,let
will not:When Using
let
The
let
keyword attaches the variable declaration to the scope of whatever block (commonly a{ .. }
pair) it's contained in. In other words,let
implicitly hijacks any block's scope for its variable declaration.let
variables cannot be accessed in thewindow
object because they cannot be globally accessed.When Using
var
var
and variables in ES5 has scopes in functions meaning the variables are valid within the function and not outside the function itself.var
variables can be accessed in thewindow
object because they cannot be globally accessed.If you want to know more continue reading below
one of the most famous interview questions on scope also can suffice the exact use of
let
andvar
as below;When using
let
This is because when using
let
, for every loop iteration the variable is scoped and has its own copy.When using
var
This is because when using
var
, for every loop iteration the variable is scoped and has shared copy.