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
?
let is a part of es6. These functions will explain the difference in easy way.
The main difference is the scope difference, while let can be only available inside the scope it's declared, like in for loop, var can be accessed outside the loop for example. From the documentation in MDN (examples also from MDN):
Also don't forget it's ECMA6 feature, so it's not fully supported yet, so it's better always transpiles it to ECMA5 using Babel etc... for more info about visit babel website
This article clearly defines the difference between var, let and const
https://medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75#.esmkpbg9b
let
Block scope
Variables declared using the
let
keyword are block-scoped, which means that they are available only in the block in which they were declared.At the top level (outside of a function)
At the top level, variables declared using
let
don't create properties on the global object.Inside a function
Inside a function (but outside of a block),
let
has the same scope asvar
.Inside a block
Variables declared using
let
inside a block can't be accessed outside that block.Inside a loop
Variables declared with
let
in loops can be referenced only inside that loop.Loops with closures
If you use
let
instead ofvar
in a loop, with each iteration you get a new variable. That means that you can safely use a closure inside a loop.Temporal dead zone
Because of the temporal dead zone, variables declared using
let
can't be accessed before they are declared. Attempting to do so throws an error.No re-declaring
You can't declare the same variable multiple times using
let
. You also can't declare a variable usinglet
with the same identifier as another variable which was declared usingvar
.const
const
is quite similar tolet
—it's block-scoped and has TDZ. There are, however, two things which are different.No re-assigning
Variable declared using
const
can't be re-assigned.Note that it doesn't mean that the value is immutable. Its properties still can be changed.
If you want to have an immutable object, you should use
Object.freeze()
.Initializer is required
You always must specify a value when declaring a variable using
const
.There are some subtle differences --
let
scoping behaves more like variable scoping does in more or less any other languages.e.g. It scopes to the enclosing block, They don't exist before they're declared, etc.
However it's worth noting that
let
is only a part of newer Javascript implementations and has varying degrees of browser support.Here's an example to add on to what others have already written. Suppose you want to make an array of functions,
adderFunctions
, where each function takes a single Number argument and returns the sum of the argument and the function's index in the array. Trying to generateadderFunctions
with a loop using thevar
keyword won't work the way someone might naïvely expect:The process above doesn't generate the desired array of functions because
i
's scope extends beyond the iteration of thefor
block in which each function was created. Instead, at the end of the loop, thei
in each function's closure refers toi
's value at the end of the loop (1000) for every anonymous function inadderFunctions
. This isn't what we wanted at all: we now have an array of 1000 different functions in memory with exactly the same behavior. And if we subsequently update the value ofi
, the mutation will affect all theadderFunctions
.However, we can try again using the
let
keyword:This time,
i
is rebound on each iteration of thefor
loop. Each function now keeps the value ofi
at the time of the function's creation, andadderFunctions
behaves as expected.Now, image mixing the two behaviors and you'll probably see why it's not recommended to mix the newer
let
andconst
with the oldervar
in the same script. Doing so can result is some spectacularly confusing code.Don't let this happen to you. Use a linter.