const, let or var: Which should I use?

Reading Time: 3 minutes 🕑

Last updated: September 18, 2021.

Javascript provides three different ways to declare a variable: const, let and var.

A quick spoiler: there is no correct answer here. Each one functions differently and can thus be used in a different context. But, in general, const and let, which were introduced with in ES6 (2015), are preferred over var in modern scripts where possible.

Once upon a time, there was only ‘var’

Prior to the introduction of const and let with ES6 in 2015, it was only possible to create a new variable using var.

var is in fact very flexible. For example, unlike const and let, it can be redeclared and Javascript will not throw and error. The second declaration simply overwrites the first.

var testVariable = "Some text"
alert(testVariable)

var testVariable = "Some other text"
alert(testVariable)

A second important difference is scope. A variable declared outside a function with var has global scope. This includes inside a for loop or a conditional if statement:

if (true) {
    var message = "Message from inside a function";
  }
  
  alert(message) // Message from inside a function
for (var i = 0; i < 10; i++) {
    var message = "Message from inside a for loop";
}
  
alert(i);   // 10
alert(message); // "Message from inside the for loop"

Only inside a function is var variable restricted in scope:

function sendMessage() {
    var message = "Message from within the function"
}

alert(message) // Undefined

At first glance, the flexibility of redeclaring variables and global scope outside of functions can seem to be an advantage. However, it also creates the possibility of errors and bugs in our code. For example, we may want Javascript to throw an error to alert us to the fact that we are about the overwrite a varibable. And though variables with global scope are accessible, we may not always need this. And when we don’t, using var creates an unnecessary clutter of globally available variables, creating the risk of bugs due to scope leakage.

Using ‘let’ and ‘const’

let and const were introduced with ES6 (2015) to overcome this issue.

Once a variable is declared with let or const, it cannot be declared again. If we try this with either, Javascript will throw an error:

const testVariable = "First declaration"
const testVariable = "Second declaration" // Error is thrown

alert(testVariable) // Not read

And both have block scope. For example, if we use them inside a for loop, they are not available then outside of it:

for (let i = 0; i < 10; i++) {
    const message = "Message from inside a for loop";
}

alert(i) // Undefined, stops code
alert(message) // Also undefined, would stop code

They are also not available outside a conditional if statement:

if (true) {
    const message = "Message from inside a function";
  }
  
  alert(message) // Message not defined

When used in a function, they are also, like var, limited to use within the function.

The difference between ‘let’ and ‘const’

As a rule of thumb, let is used when the values inside it are expected to change. For example, we use let when declaring a looping variable. If we tried this using const, Javascript would throw an error:

for (const i = 0; i < 10; i++) { // fails on the first line!
    const message = "Message from inside a for loop";
}

alert(message)

In this way, let is much more flexible as it is possible to reassign its values. This makes a little bit like the modern equivalent of var, though with more limited scope and the throwing of an error when trying to overwrite a previously declared variable.

On the other hand, const should be used when variable values are not expected to change. Though this is possible to a limited extent, the values of the const variable cannot be reassigned. Thus, a good rule of thumb is to use const when we want values to remain the same:

let days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
alert(days) // "Mon,Tue,Wed,Thu,Fri"

days = ["Sat", "Sun"]
alert(days) // "Sat,Sun"
const days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
alert(days) // "Mon,Tue,Wed,Thu,Fri"

days = ["Sat", "Sun"] // Error occurs here. Code stops executing
alert(days)

Conclusion

There is no ‘correct’ answer to the question of when one should use let, const and var. However, it is increasingly considered best practice to use let and const over var in modern scripting due to their strict behavioural properties and thus potential to catch bugs in longer scripts. However, var still remains a popular choice amongst some developers due to its flexibility.

So, when writing new scripts for scratch, we recommend let and const. But it is still important to be familiar with var and its behaviour, especially when working with older scripts.