var
s are scoped to function
s, and they can "see" outside their functions to the outer context.var
still works that way, using functions as containers, but there are two new ways to declare variables: const
and let
.const
and let
use {
and }
blocks as containers, hence "block scope". Block scoping is most useful during loops. Consider the following:let
works like var
in the sense that its data is read/write. let
is also useful when used in a for loop. For example, without let, the following example would output 5,5,5,5,5
:let
instead of var
, the value would be scoped in a way that people would expect.const
is read-only. Once const
has been assigned, the identifier cannot be reassigned.const
.const
reference, below we've switch to using let for the literal object.