A variable in JavaScript can be defined using three different keywords, let
, var
and const
, or automatically, which is a bad practice.
To declare a variable automatically, one could just assign a value to it. Because JavaScript is loosely typed, it will automatically assign a data type to it.
Using var
The var
keyword was the original way of declaring variables until 2015, where the other keywords were introduced.
Itās not a good practice to use it nowadays, and should be only used when developing for older browsers and applications.
Using let and const
For most use cases, const
should be always preferred. It creates an immutable variable, meaning itās value cannot be changed after the definition.
For cases where the variable will be reassigned, we use the let
keyword. A variable declared with the let
keyword can be reassigned normally, to any other type.
Declaring undefined variables
If declaring a variable using the let
keyword without assigning a value to it, it creates an undefined variable, which is a variable that donāt have any value.
Undefined vs null
An undefined variable is not the same as a variable assigned to null. Each one returns different values.
For example: