Description
This declaration creates a constant whose scope can be either global or local to the
block in which it is declared. Global constants do not become
properties of the window
object, unlike var
variables.
An initializer for a constant is required. You must specify its value in the same declaration. (This makes sense, given that it can't be changed later).
The const
declaration creates a read-only reference to a
value. It does not mean the value it holds is immutable - just that the
variable identifier cannot be reassigned. For instance, in the case where the content is
an object, this means the object's contents (e.g., its properties) can be altered.
All the considerations about the
temporal dead zone
apply to both let
and const
.
A constant cannot share its name with a function or a variable in the same scope.
Unlike var
, const
begins declarations, not statements. That means you cannot use a lone const
declaration as the body of a block (which makes sense, since there's no way to access the variable).
if (true) const a = 1; // SyntaxError: Unexpected token 'const'