Demystifying "const" variables in JavaScript

Sunny Singh on June 09, 2019

By now, ES6 (ES2015) is widespread enough that many developers are writing their JavaScript with this modern syntax. However, I commonly see room f... [Read Full]
markdown guide
 

As a common rule, I suggest using const to declare all new variables, and when you come across a point where you need to reassign one of those variables, then change it to a let.

Great explanation! I will definitely be trying this, as I have always been confused about const and when to use it. But looking at it “upside down” is a great approach. Thanks!

 

It's always been my rule of thumb.

Use const. If you need to reassign it, change the variable to let. No reason to use var anymore.

 

One reason to use var: prototype something in the browser console and get something wrong, for that session your const won't be changeable, so use var for fast prototyping

 

Agreed, defaulting to const is useful if because then when you see a let you know it must get reassigned.

More generally, sometimes variables are "values," and sometimes they're "places," and to me it's worth an extra two characters to be clear about which I'm using.

It's not about avoiding any sort of actual risk -- just about writing code that better expresses its intent.

 

I use const all the time. It's very rare that I need let. I can see let being used as an index in a for loop, but I almost never need a for loop. Filter, map and reduce usually do the trick.

 

Functional and immutable Js makes code more readable and maintainable imo. I almost exclusively use const, I have yet to find a scenario where I need to change this.

 

We're in the same boat! I try to avoid let and even loops, which people think I'm crazy for.

 

Map, Filter and Reduce are loops, but they are immutable for the purpose of us devs.

They're declarative loops, but yes definitely loops.

 

Thanks for the post. I use const only for constants in top-level scope like const URL = 'https://example.com'.
For other scenarios I prefer to use let. It's just annoying to change const to let every time you need to make a variable mutable.

This post by TC39 committee member Jamie Kyle is rude, but resonates with my view on let vs const battle.

 

Thanks for sharing that post.

I'm not sure I can fully agree with Kyle. Most of his points are fair, but the main goal of const is to communicate intention. Yes if I didn't reassign my variable, then I intend on it not to change. Later on when I come back and want to change it that's fine, I just know that it's not being changed currently so I can be more aware of that.

 

Agreed, that post misses the point.

Why am I not surprised that someone like that is on the language committee? (I'd say JS culture is basically worse is better, but somehow its movers and shakers manage to screw up even that up.)

 

To add to this - if you want your object to actually be immutable, you can use Object.freeze. If the object contains other objects, this will only freeze the first level of values. You can use the deep-freeze package to recursively freeze the object.

 

Great article! My boot camp instructor also advocates a "const first" approach, for all the reasons you outlined here. I'd rather get a "reassignment to a constant variable" error, which is easily identified and fixed, then trying to figure out why my code is broken due to an inadvertent reassignment.

 

99% of the time your variable reference won't need to change (reassigned) so use const, for the other use let, it's that simple.

 

People overuse let a lot, it feels like var to them so keep using it, really nicely pointed out.

 
 

Not OP, but I mentally distinguish between "values," which use const but tend to be local / temporary, and "constants," which also use const but tend to be in outer scope. I use screaming snake case only for the latter, if at all.

IMO when most of your values are immutable, which is a good place to be, the constants stop being special. Instead, the the things that can change over time are special, as they should be, since they require more effort to reason about.

 

I do, for real constants that are static values. Consider this example:

const BASE_URL = 'http://example.com';

const url = `${BASE_URL}/hello`;

Since url isn't a true constant (it uses another variable to determine its value), I keep it lowercase.

Also, I only use constant case at the top level scope, or when I'm importing constants from another file.

 

Overcome imposter syndrome on this topic. Thank you ! Great article !

 
code of conduct - report abuse