Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "const" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle illustrates the potential semantic issues of using const.

Preview
Close

Transcript from the "const" Lesson

[00:00:00]
>> Kyle Simpson: Since it was asked earlier, let's segue into a discussion of const. Const is by most accounts even better than let. You should not even use let and you should only use const wherever possible. So the problem that I have with const is essentially the const keyword does not carry its own weight within the language.

[00:00:24]
>> Kyle Simpson: That's a phrase that is sometimes used in specifications or standards. When somebody says, yeah, there might be a benefit, but the cost is greater and so it's not worth it. And what I'm saying is, that there might be a slight benefit to const, it is not as significant as you have been led to believe, and there is a cost to const which is in my opinion greater.

[00:00:49]
It doesn't carry its own weight. That doesn't mean I never use it, it means I am very sparing in when I use it and I only use it in very specific cases. As opposed to it's my general replacement for all declarations. So, the big cost that comes with const is not even unique to JavaScript.

[00:01:09]
If you go on Stack Overflow right now and search for problems with the const keyword, you'll find tens of thousands of posts across a whole variety of different languages. Because it turns out, every time the const keyword has ever been added to any programming language, programmers got confused.

[00:01:26]
And I think one of the reasons they get confused, is because when we think of const, and we think of the word constant. What we're thinking to ourselves is, a thing that doesn't change.
>> Kyle Simpson: But that's not what a programmer means by const, or at least that's not what a programming language designer means by const.

[00:01:43]
A programming language designer means a variable that can't be reassigned, and those are two entirely different things. Here, on line 4, am declaring a const, a myTeacher that can't be reassigned. So I get an error on line 5, but look what happens when I declare a mutable value like an array.

[00:02:05]
I'm not trying to reassign teachers on line 8, so I get no error, but I am actually still mutating the value which is totally allowed. This creates all kinds of frustration, as a matter of fact, some languages have struggled so much with this. Like for example Java, they removed to the const keyword and put in another keyword called final.

[00:02:28]
Just so they could sort of separate themselves from the baggage of people thinking that this thing meant something that it didn't. I'm not sure that final is better, but that's what they did, okay? So, there is a baggage that comes with const. And a new reader to your code unless they are already well familiar with the concept of const, and the idea of the difference between mutable values and mutable assignments.

[00:02:52]
It is more likely than not that a reader could get tripped up when they see you say const teachers equals and then they see something like an array, they are most likely takeaway that they would get or the most likely impression that they would get is that the array won't change.

[00:03:09]
That it won't mutate itself, that's not what you were saying. You were saying, no, I'm just not gonna reassign the variable, so that when you mutate the array, all of a sudden they get confused. So the const keyword has this problem to it, it has this baggage. Now, you're supposed to use the const keyword within a very small block, right?

[00:03:31]
You're supposed to keep your block small and you're supposed to use the const key word within small blocks. What the const keyword is actually saying, from a semantic perspective is, for the rest of this block, I promise it's not gonna get reassigned. I don't think that reassignment is actually a big problem.

[00:03:53]
That isn't the source of bugs typically in my program. But even if it were a source of bugs, if I use the const inside of a small block of three to five lines of code, what I am at best telling the user is, guess what, on the next four lines of code I'm not gonna reassign it.

[00:04:10]
Cuz there is only four more lines of code in the entire program that could reassign it, because of how lexical scoping works,
>> Kyle Simpson: The const keyword pretends as if it's doing this major awesome thing for us, and it's not actually doing that much at all.
>> Kyle Simpson: Not solving that many problems.

[00:04:28]
So it has this problems, and it's not really solving the problem that it pretends, it's kinda like a night lighting in your child's bedroom. It definitely makes your child feel better that there's no monsters in the closet, but we all know there's no monsters in the closet. So it makes you feel good, but I'm not sure that it really is carrying its own weight with the potential of confusion that comes.

[00:04:55]
So, all of that taken into consideration, my personal take on const is that I only use it when I am assigning a value that is already a primitive and therefore immutable. So I will only use const with things like strings, Booleans, or numbers. And I use it as a semantic place holder for those literals.

[00:05:15]
So if have the number 13, and that means something special and magical in my program, I'll give it the name special magical in a const declaration instead of just using 13. That's what constants are supposed to be for. They're supposed to give semantic meaning as placeholders. I don't think we need a const to prevent us from accidentally reassigning things.

[00:05:39]
I just don't think it's that big of a problem, at least in the general sense. That's my take on const, where many of the developers out there will tell you that you should use const. And then you should use let and you should never use var. My advice is reversed, you should default to using var.

[00:06:02]
Use let where it is helpful, use const sparingly only with immutable primitive values.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now