What’s the benefit of const?

So let's return to the question we asked last time: what should you declare most of your variables with, let or const?

In most cases, const is the best choice, not because it allows you to do the most, but because it lets you do the least. This may sound a bit strange at first, but we'll see the reasoning behind this statement in this video:

const has restrictions that let doesn't, and as a result they make your code more readable.

const must be initialized

In general, const works just like that of let but with two important differences:

  1. it must be initialized with a value (as we saw in the last video), and
  2. that value cannot be reassigned after declaration. Attempting to modify a const variable will result in an error.

Let's revisit our age variable, declared with const. We initialize it to 26, but if we want to change it to 36, for example. What do you think will happen?

const age = 26;
age = 36; // TypeError: assignment to a constant

We get a type error telling us we can't reassign our const variable, as we would expect.

const signals what you plan to do

const is a great default choice precisely because it can’t be reassigned. When you assign a value, you aren’t just declaring a piece of information, you are also declaring your intent. Meaning, you’re also telling other developers what you plan to do with that information.

When you create a variable and indicate that it shouldn't be changed by using const, you're telling others that they can without even looking at the rest of the program, know that it's going to stay the same. This is as compared to variables made with let and especially with var. Since they can be reassigned, they can be changed at any time within your program.

Let's say we're looking through two identical programs, looking for an error that's popped up. The first program uses const to assign a variable while the second uses var to assign a variable:

 const originalPrice = 50
​ ​const​ percentOff = 20;
​ ​const​ salePrice = originalPrice * (percentOff / 100);

​ ​// rest of our program (maybe hundreds of lines of code)

​ ​return​ ​salePrice;
var originalPrice = 50var percentOff = 20;var salePrice = originalPrice * (percentOff / 100);

​ ​/* rest of our program
  ...
  (maybe hundreds of lines of code)​
  ...
*/

​ ​return​ ​salePrice;

Which one is easier to read (and as a result find the bug in)? Why?

Both programs are identical except for how the two variables are declared, how the first is much easier to understand.

With the first program, you know exactly what will get returned, no matter what code comes afterwards: the calculated sale price, that is 20 percent off from the original price. You know this because sale price is a const variable that can’t be reassigned before it reaches the end.

With the second program, however you have no idea what the return value is going to be. You are going to need to go through all of additional code looking for reassignments or conditionals of functions or anything that might change the value. Maybe the code has another discount applied. Maybe there is a coupon code that will be added later that will discount the product even more. Maybe this sale is invalid.

Whatever might be the case, you truly don't know what the final value of sale price is going to be when it’s assigned with var. When you assign a variable with const, it removes one additional piece of information that you need to retain in your head while reading code.

Let's make one small change to our original program. Let's declare salePrice with let and not const:

 const originalPrice = 50
​ ​const​ percentOff = 20;let salePrice = originalPrice * (percentOff / 100);

​ ​/* rest of our program
  ...
  (maybe hundreds of lines of code)​
  ...
*/

​ ​return​ ​salePrice;

Take a moment and think about what you can be certain of from this code. You know you can’t be sure of the total. The developers have signaled that original price and percent off can't changed, but the sale price can.

Review

So by thinking about this code, we know what the right choice is for declaring most of our variables going forward--const. We want to start with const because we don't want to signal to others that a value can be reassigned or changed when it shouldn't. This help guarantee that values are more predictable and not subject to unintended changes.

Note that const works best with our basic data types, called primitives, like numbers, strings, and booleans but there is a vulnerability it has when working with non-primitive data types, namely objects. We'll cover that in time.

So going forward, use const as your choice when declaring variables. And if ever you need to reassign a variables value, just switch const to let. It's as simple as that.