*In Javascript, there are 3 keywords you can use to declare a varible:
var
let
const
Starting with ES6, the let
and const
keywords are used, making the var
keyword almost obsolete in usage. Using let
and const
is preferred.
When a variable is assigned a value with one of the above keywords, the variable is said to be initialized with the assigned value
let
- Variables assigned a value using the
let
keyword can be re-assigned to a different value- You can declare a variable with
let
without assigning a value to the variable name
> In this case, the variable is automatically initialized with a default value ofundefined
.
The var
keyword functions similarly to let
const
- Variables declared with
const
keyword cannot be re-assigned
>const
stands for "constant"
> trying to re-assign a value to aconst
declared variable yieldsTypeError
- A
const
variable must be initialized with a value
> declaring aconst
variable without a value yieldsSyntaxError