let
const
let
and const
const
creates immutable variables
const
does not make the value immutableconst
in loop bodiesvar
-declared variableslet
-declared variablestypeof
throws a ReferenceError
for a variable in the TDZlet
and const
in loop heads
for
loopfor-of
loop and for-in
loopconst
versus let
versus var
ES6 provides two new ways of declaring variables: let
and const
, which mostly replace the ES5 way of declaring variables, var
.
let
let
works similarly to var
, but the variable it declares is block-scoped, it only exists within the current block. var
is function-scoped.
In the following code, you can see that the let
-declared variable tmp
only exists inside the block that starts in line A:
const
const
works like let
, but the variable you declare must be immediately initialized, with a value that can’t be changed afterwards.
Since for-of
creates one binding (storage space for a variable) per loop iteration, it is OK to const
-declare the loop variable:
The following table gives an overview of six ways in which variables can be declared in ES6 (inspired by a table by kangax):
Hoisting | Scope | Creates global properties | |
---|---|---|---|
var |
Declaration | Function | Yes |
let |
Temporal dead zone | Block | No |
const |
Temporal dead zone | Block | No |
function |
Complete | Block | Yes |
class |
No | Block | No |
import |
Complete | Module-global | No |
let
and const
Both let
and const
create variables that are block-scoped – they only exist within the innermost block that surrounds them. The following code demonstrates that the const
-declared variable tmp
only exists inside the block of the if
statement:
In contrast, var
-declared variables are function-scoped:
Block scoping means that you can shadow variables within a function:
const
creates immutable variables Variables created by let
are mutable:
Constants, variables created by const
, are immutable – you can’t assign different values to them:
const
does not make the value immutable const
only means that a variable always has the same value, but it does not mean that the value itself is or becomes immutable. For example, obj
is a constant, but the value it points to is mutable – we can add a property to it:
We cannot, however, assign a different value to obj
:
If you want the value of obj
to be immutable, you have to take care of it, yourself. For example, by freezing it:
Object.freeze()
is shallow Keep in mind that Object.freeze()
is shallow, it only freezes the properties of its argument, not the objects stored in its properties. For example, the object obj
is frozen:
But the object obj.foo
is not.
const
in loop bodies Once a const
variable has been created, it can’t be changed. But that doesn’t mean that you can’t re-enter its scope and start fresh, with a new value. For example, via a loop:
There are two const
declarations in this code, in line A and in line B. And during each loop iteration, their constants have different values.
A variable declared by let
or const
has a so-called temporal dead zone (TDZ): When entering its scope, it can’t be accessed (got or set) until execution reaches the declaration. Let’s compare the life cycles of var
-declared variables (which don’t have TDZs) and let
-declared variables (which have TDZs).
var
-declared variables var
variables don’t have temporal dead zones. Their life cycle comprises the following steps:
var
variable is entered, storage space (a binding) is created for it. The variable is immediately initialized, by setting it to undefined
.undefined
.let
-declared variables Variables declared via let
have temporal dead zones and their life cycle looks like this:
let
variable is entered, storage space (a binding) is created for it. The variable remains uninitialized.ReferenceError
.undefined
.const
variables work similarly to let
variables, but they must have an initializer (i.e., be set to a value immediately) and can’t be changed.
Within a TDZ, an exception is thrown if a variable is got or set:
If there is an initializer then the TDZ ends after the initializer was evaluated and the result was assigned to the variable:
The following code demonstrates that the dead zone is really temporal (based on time) and not spatial (based on location):
typeof
throws a ReferenceError
for a variable in the TDZ If you access a variable in the temporal dead zone via typeof
, you get an exception:
Why? The rationale is as follows: foo
is not undeclared, it is uninitialized. You should be aware of its existence, but aren’t. Therefore, being warned seems desirable.
Furthermore, this kind of check is only useful for conditionally creating global variables. That is something that you don’t need to do in normal programs.
When it comes to conditionally creating variables, you have two options.
Option 1 – typeof
and var
:
This option only works in global scope (and therefore not inside ES6 modules).
Option 2 – window
:
There are several reasons why const
and let
have temporal dead zones:
const
: Making const
work properly is difficult. Quoting Allen Wirfs-Brock: “TDZs … provide a rational semantics for const
. There was significant technical discussion of that topic and TDZs emerged as the best solution.” let
also has a temporal dead zone so that switching between let
and const
doesn’t change behavior in unexpected ways.undefined
before its declaration then that value may be in conflict with the guarantee given by its guard.Sources of this section:
let
and const
in loop heads The following loops allow you to declare variables in their heads:
for
for-in
for-of
To make a declaration, you can use either var
, let
or const
. Each of them has a different effect, as I’ll explain next.
for
loop var
-declaring a variable in the head of a for
loop creates a single binding (storage space) for that variable:
Every i
in the bodies of the three arrow functions refers to the same binding, which is why they all return the same value.
If you let
-declare a variable, a new binding is created for each loop iteration:
This time, each i
refers to the binding of one specific iteration and preserves the value that was current at that time. Therefore, each arrow function returns a different value.
const
works like var
, but you can’t change the initial value of a const
-declared variable:
Getting a fresh binding for each iteration may seem strange at first, but it is very useful whenever you use loops to create functions that refer to loop variables, as explained in a later section.
for-of
loop and for-in
loop In a for-of
loop, var
creates a single binding:
const
creates one immutable binding per iteration:
let
also creates one binding per iteration, but the bindings it creates are mutable.
The for-in
loop works similarly to the for-of
loop.
The following is an HTML page that displays three links:
What is displayed depends on the variable target
(line B). If we had used var
instead of const
in line A, there would be a single binding for the whole loop and target
would have the value 'vielleicht'
, afterwards. Therefore, no matter what link you click on, you would always get the translation 'vielleicht'
.
Thankfully, with const
, we get one binding per loop iteration and the translations are displayed correctly.
If you let
-declare a variable that has the same name as a parameter, you get a static (load-time) error:
Doing the same inside a block shadows the parameter:
In contrast, var
-declaring a variable that has the same name as a parameter does nothing, just like re-declaring a var
variable within the same scope does nothing.
If parameters have default values, they are treated like a sequence of let
statements and are subject to temporal dead zones:
The scope of parameter default values is separate from the scope of the body (the former surrounds the latter). That means that methods or functions defined “inside” parameter default values don’t see the local variables of the body:
JavaScript’s global object (window
in web browsers, global
in Node.js) is more a bug than a feature, especially with regard to performance. That’s why it makes sense that ES6 introduces a distinction:
var
declarationslet
declarationsconst
declarationsNote that the bodies of modules are not executed in global scope, only scripts are. Therefore, the environments for various variables form the following chain.
Function declarations…
let
.var
.The following code demonstrates the hoisting of function declarations:
Class declarations…
Classes not being hoisted may be surprising, because, under the hood, they create functions. The rationale for this behavior is that the values of their extends
clauses are defined via expressions and those expressions have to be executed at the appropriate times.
const
versus let
versus var
I recommend to always use either let
or const
:
const
. You can use it whenever a variable never changes its value. In other words: the variable should never be the left-hand side of an assignment or the operand of ++
or --
. Changing an object that a const
variable refers to is allowed:
You can even use const
in a for-of
loop, because one (immutable) binding is created per loop iteration:
Inside the body of the for-of
loop, x
can’t be changed.
let
– when the initial value of a variable changes later on.
var
.If you follow these rules, var
will only appear in legacy code, as a signal that careful refactoring is required.
var
does one thing that let
and const
don’t: variables declared via it become properties of the global object. However, that’s generally not a good thing. You can achieve the same effect by assigning to window
(in browsers) or global
(in Node.js).
An alternative to the just mentioned style rules is to use const
only for things that are completely immutable (primitive values and frozen objects). Then we have two approaches:
const
: const
marks immutable bindings.let
: const
marks immutable values.I lean slightly in favor of #1, but #2 is fine, too.