profile
Juan Vega
Software Developer | Writer | Nerd

ES6: let vs. const in JavaScript

ES6 was released on June 2015 and with it came two new methods for declaring variables in JavaScript, let and const. Together they quickly replaced var as the method of choice for declaring variables due their added abilities.

With that said, how do you know which one to use though? They both offer block scope, allow you to store data and can only be declared once in a given scope.

The only concrete difference between them being that the value of let can be changed while that of const cannot.

The key thing to keep in mind when deciding which to use is how you plan on using that variable. Take for example the following line of code.


              let myName = "Your Name";

              function printMyName(){
                console.log(myName);
              }
            

In this simple application you are declaring a variable that contains your name and then you are using a function to print it out to the console.

Your name is not something that’s going to change anytime soon so anything that tries to do so would be considered dangerous in the application. With the current code however, there is nothing stopping it from happening. You could run the following and it would work just fine.


              let myName = "Your Name";
              myName = “Your Changed Name”;

              function printMyName(){
                console.log(myName);
              }
            

While it’s something that you wouldn't do on purpose, it is something that can occur. A better way of writing the above code would have been using the const variable


              const myName = "Your Name";
              myName = “Your Changed Name”;   //Gives error “Assignment to constant variable”

              function printMyName(){
                console.log(myName);
              }
            

Now if you ran the code, you would get a more appropriate response in the form of an error. This way you can make sure that your name will never change unless you go back and make a deliberate decision to do so during the initial declaration.

From here you can deduce that you should use the const variable declaration method when you know that the value that it references will not change later on. This way you can keep your application safe from unwanted mutations.

Now take a look at this example.


              for(let i = 0; i < 5; i++){
                console.log(i);
              }
            

This is a simple for loop that will print out the numbers 1 to 5 onto the browser. You declared the iterator variable using the let keyword and if you ran the code, it would work just fine.

You would see


              0
              1
              2
              3
              4
            

displayed on the console.

If you ran this however,


              for(const i = 0; i < 5; i++){
                console.log(i);
              }
            

the code would not run and you would once again get an “Assignment to constant variable” error. In this case, not the intended outcome you wanted.

The reason being that only let can be reassigned values. Const cannot.

Keep all of this mind when trying to decide which method to use and for the most part, only use let when you know the value will change. Such as in arithmetic operations. Otherwise, it’s best to use const and only change it let if you need to later on.