Understanding the Different Variable Declarations in JavaScript: var, let, and const
Table of contents
No headings in the article.
In JavaScript, variables are declared using the var
, let
, and const
keywords. Each of these keywords has its own unique characteristics that determine how the variable can be used in the code.
One of the main differences between these three keywords is the scope. Variables declared with var
are function-scoped, whereas variables declared with let
and const
are block-scoped.
The var
keyword was the original way of declaring variables in JavaScript, but it has some quirks that can lead to unexpected behaviour. One such quirk is variable hoisting, which means that the declaration of a var
variable is moved to the top of its scope during the compilation phase. However, the assignment remains in place. This means that you can access the variable before it is explicitly declared, but the value will be undefined
. This can lead to bugs and unexpected behaviour in your code.
The let
and const
keywords were introduced in ES6 as a way to address the shortcomings of var
. When you declare a variable with let
or const
, its declaration is hoisted to the top of the block, just like with var
. However, the difference is that the variable is not initialized until the line on which it is declared is executed. This means that if you try to access a let
or const
variable before it is declared in the current block, you will get a ReferenceError
.
Consider the following code:
console.log(a); // undefined
var a = 1;
console.log(b); // ReferenceError: b is not defined
let b = 2;
console.log(c); // ReferenceError: c is not defined
const c = 3;
When this code is executed, the following happens:
During the compilation phase, the
var
declaration fora
is hoisted to the top of the global scope. At this point,a
is declared but not yet initialized, so its value isundefined
.When the first
console.log
statement is executed,a
is accessed, but its value isundefined
.Next, the
let
andconst
declarations forb
andc
are also hoisted to the top of the block (in this case, the global scope), but unlike withvar
, they are not initialized. This means that attempting to access them before their declaration will result in aReferenceError
.When the second
console.log
statement is executed, attempting to accessb
before its declaration causes aReferenceError
.Similarly, when the third
console.log
statement is executed, attempting to accessc
before its declaration causes aReferenceError
.
Here's a visual representation of the hoisting behaviour:
Compilation Phase:
------------------
var a;
let b; // not initialized
const c; // not initialized
Execution Phase:
----------------
console.log(a); // undefined
a = 1;
console.log(b); // ReferenceError: b is not defined
b = 2;
console.log(c); // ReferenceError: c is not defined
c = 3;
Finally, const
variables have one more restriction than let
variables. Once a value is assigned to a const
variable, it cannot be reassigned. This makes const
variables useful for defining constants in the code.
In general, it's a good practice to use let
and const
instead of var
whenever possible, as they provide better scoping and can help avoid unexpected behaviour in your code.