let, const and var in JavaScript

There are three different ways of declaring a variable in JavaScript :

  1. let

  2. const

  3. var

let and const were introduced in ES6, while var is the older way of declaring variables in JavaScript.

let

We use the let keyword for variables whose value we intend to change later on in our code.

Eg:

let sum = 0;
sum = sum + 10;

i.e, We use let to declare mutable variables.

This also means let is also the way to go when we need to declare an empty variable and assign its value later on in the code.

Eg:

let score;
score = 100;

const

We use const keyword for variables whose value we don’t intend to change later on in our code.

Eg:

const sum = 0;
sum = sum + 10; // This isn’t allowed and JavaScript will throw an error

i.e, We use const to declare immutable variables.

This also means we can’t use const to declare an empty variable.

Eg:

const score; // This isn’t allowed and JavaScript will throw an error

In short, let is used to declare mutable variables and const is used for immutable variables. Also, empty variables can be declared using let but not with const.

Should I use let or const ?

As a good programming practice, use const by default. Use let only when you are sure that you need to change the value of the variable later on in the code.

This is because changing the value of a variable can potentially create bugs. So we need to make sure that there are as few variable mutations as possible.

var: the third way

As a good programming practice, completely avoid using the var keyword to declare variables in your code. Use only let and const.

However, we need to know how var works inorder to deal with the code written before ES6. On the surface, var is pretty much similar to let. Just like let, var can be used to declare mutable variables and empty variables too.

But under the hood, they are actually pretty different. let is block-scoped while var is function scoped. They also differ in the way they are hoisted.

To know more about why let and const is preferred over var, read this article by Linda Vivah.

Why is var still not removed from JavaScript ?

Since JavaScript is an interpreted language, backwards compatibility is absolutely necessary. There are many pages on the internet that were written before the introduction of ES6 and many of them won’t be rewritten. If we remove var from JavaScript, all these pages will get broken.

Summary

let, const and var are the three ways you can declare a variable in JavaScript. var is the older way of declaring variables while let and const were introduced in ES6 to solve the issues that arose due to the use of var.

By default, you should always try to use const to declare variables as a good programming practice. However, const can be used to declare only immutable variables. To declare mutable variables and empty variables, use let. Avoid using var to declare variables in your code. var is still there in JavaScript only to ensure backwards compatibility of the language.

Thanks for reading :)