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!
I am a software development engineer in test for Infosys. My job is officially to write automated tests in Selenium Webdriver. I'm also a web developer as a hobbyest
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
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
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.
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.
Full stack web dev.
Studying FP web development approaches, while helping Mission Bit create paths to programming for underserved public school kids.
Previously @ Gradescope.
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.
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.
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.
Full stack web dev.
Studying FP web development approaches, while helping Mission Bit create paths to programming for underserved public school kids.
Previously @ Gradescope.
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.)
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.
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
Full stack web dev.
Studying FP web development approaches, while helping Mission Bit create paths to programming for underserved public school kids.
Previously @ Gradescope.
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.
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
Great explanation! I will definitely be trying this, as I have always been confused about
constand 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
I use
constall the time. It's very rare that I needlet. I can seeletbeing 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
letand even loops, which people think I'm crazy for.Map,FilterandReduceare loops, but they are immutable for the purpose of us devs.They're declarative loops, but yes definitely loops.
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 thedeep-freezepackage to recursively freeze the object.Agreed, defaulting to
constis useful if because then when you see aletyou 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.
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 changeconsttoletevery 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 constbattle.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
constis 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.)
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.
People overuse let a lot, it feels like var to them so keep using it, really nicely pointed out.
Overcome imposter syndrome on this topic. Thank you ! Great article !
Awesome to hear 😁
Do you like constant case?
Not OP, but I mentally distinguish between "values," which use
constbut tend to be local / temporary, and "constants," which also useconstbut 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:
Since
urlisn'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.
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.