DEV Community

Cover image for Demystifying "const" variables in JavaScript

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...
Collapse
 
cecilelebleu profile image
Cécile Lebleu

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!

Collapse
 
jckuhl profile image
Jonathan Kuhl

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.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

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

Collapse
 
nickytonline profile image
Nick Taylor

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.

Collapse
 
georgecoldham profile image
George

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.

Collapse
 
sunnysingh profile image
Sunny Singh

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

Collapse
 
georgecoldham profile image
George

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

Thread Thread
 
sunnysingh profile image
Sunny Singh

They're declarative loops, but yes definitely loops.

Collapse
 
karataev profile image
Eugene Karataev • Edited

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.

Collapse
 
sunnysingh profile image
Sunny Singh

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.

Collapse
 
johncip profile image
jmc • Edited

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.)

Collapse
 
joeattardi profile image
Joe Attardi

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.

Collapse
 
johncip profile image
jmc • Edited

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.

Collapse
 
ceceliacreates profile image
Cecelia Martinez

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.

Collapse
 
latheresienne profile image
Latheresienne

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

Collapse
 
sunnysingh profile image
Sunny Singh

Awesome to hear 😁

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

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.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Do you like constant case?

Collapse
 
johncip profile image
jmc • Edited

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.

Collapse
 
sunnysingh profile image
Sunny Singh • Edited

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.

Collapse
 
shubhambattoo profile image
Shubham Battoo • Edited

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