Constants and Variable Declarations
Constants and variables are declarations that bind a value and type to an identifier. Constants are initialized with a value and cannot be reassigned afterwards. Variables are initialized with a value and can be reassigned later. Declarations can be created in any scope, including the global scope.
Constant means that the identifier's association is constant, not the value itself – the value may still be changed if is mutable.
Constants are declared using the let
keyword. Variables are declared using the var
keyword. The keywords are followed by the identifier, an optional type annotation, an equals sign =
, and the initial value.
Variables and constants must be initialized.
The names of the variable or constant declarations in each scope must be unique. Declaring another variable or constant with a name that is already declared in the current scope is invalid, regardless of kind or type.
However, variables can be redeclared in sub-scopes.
A variable cannot be used as its own initial value.
Last updated