var, let, or const — Which One Should You Use?

The reality is that most new devs that are learning JavaScript for the first time are going to be more aware of let and const, but may have never had a formal introduction or understand why it is this way (especially when reading documents and old tutorials). For many devs that used JavaScript early in their careers and are coming back, looking at the ES2015 changes can be very confusing.

The primary difference with var, let, and const comes when either re-declaring or updating them. const is unable to be updated or re-declared, let is able to be updated but not re-declared, and var is able to both be re-declared and updated.

If you are accustomed to writing JavaScript, the differences in these three declarations may make perfect sense, however for many situations, young developers often struggle with determining when to use each.

When to Use const

A general rule of thumb: always use const first*, and if you need to update the value later on then you can switch to let or var as needed. This helps keep code as clear as possible when writing and reading.

*Note, this is a personal preference that is still widely debated, though the trend is leaning this way.

While it is too much for this article to go into all use cases for const , here is a very common and simple example where const will be the right fit.

const pi = 3.14159265359

You will never want the value of Pi to change (in this universe at least), therefore const would be the obvious choice for this declaration.

Using const for Objects and Arrays

When using const with objects and arrays, the variable as a whole cannot change (i.e. you can not assign a new object or array to the variable), but you can manipulate the current array or values.

It is difficult to describe this in words, so here are examples of manipulating objects and arrays that are declared with const.

Manipulating Objects Declared with const

const jedsCar = { 
make: "Honda",
model: "Accord",
color: "Green"
}

We now have an object to represent a car, that cannot be re-declared. But, let’s say I took that car into get a paint job, and now I have a purple car. Here’s what we could do:

jedsCar.color = "Purple";

now, if we look at jedsCar, we end up seeing an object like this:

{ 
make: "Honda",
model: "Accord",
color: "Purple"
}

and that’s all there is to it! Now you can have an object declared with const, that can still have new values placed in!

Manipulating Arrays Declared with const

const favoriteNums = [3, 7, 19, 27, 72];
favoriteNumes.push(12);

// [3, 7, 19, 27, 72, 12]

When to Use let and var

While it is unorthodox and incorrect to make full claims about what should be done as “best” in development, you will find that the general consensus is to use let all the time between the two.

Why is this?

The conception of let comes from a place of fixing errors that could happen with var. Using let will help you keep a block scope, allowing variables with the same name to work as long as they are under different scopes, and not allowing them to work under the same scope.

This can save a lot of headache, especially early on when learning, around accidentally redeclaring a variable. Here is an article digging deeper into why you never really need to use var, and should stick with let.

Conclusion

Currently a frontend developer. Husband and father. Looking for software engineering opportunities.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store