Understanding the Different Variable Declarations in JavaScript: var, let, and const

Understanding the Different Variable Declarations in JavaScript: var, let, and const

Table of contents

No heading

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:

  1. During the compilation phase, the var declaration for a is hoisted to the top of the global scope. At this point, a is declared but not yet initialized, so its value is undefined.

  2. When the first console.log statement is executed, a is accessed, but its value is undefined.

  3. Next, the let and const declarations for b and c are also hoisted to the top of the block (in this case, the global scope), but unlike with var, they are not initialized. This means that attempting to access them before their declaration will result in a ReferenceError.

  4. When the second console.log statement is executed, attempting to access b before its declaration causes a ReferenceError.

  5. Similarly, when the third console.log statement is executed, attempting to access c before its declaration causes a ReferenceError.

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.