I'm a Systems Reliability and DevOps engineer for Netdata Inc. When not working, I enjoy studying linguistics and history, playing video games, and cooking all kinds of international cuisine.
Other than the readability aspect (which is indeed big), there are a few other bnefits to using const that many people don't think about:
It tells the JS engine that the reference won't change. This sounds useless, but it has the potential to allow for some optimizations that would not otherwise be possible. I'm not sure whether any of the big JS implementations actually take advantage of this or not, but it's still worth considering.
Some IDE's, as well as some code checking tools, can be configured to complain if they see an indentifier declared with const on the left side of an assignment expression, even if the assignment would be to a property of the referenced object.
Honestly, if I know for certain that something shouldn't change, I use const, period. Yes, it can lead to confusion, but that confusion is usually less significant than properly annotating that something shouldn't change.
Brian Rinaldi is a Developer Experience Engineer at LaunchDarkly with over 20 years experience as a developer for the web. Brian is active in the community running CFE.dev and Orlando Devs.
Thanks. Yes, I didn't bring them up because I thought it might sidetrack the discussion of const a bit but if immutability really matters for what you are doing, then definitely.
The issue here is that there are 2 similar but different concepts: referential transparency (const) vs immutability (Object.freeze or even better recursive Object.freeze)
In practice, I do what Wes Bos had recommended in his ES6 course: use const for every single variable, and only change to let if I will need to change that variable's assignment.
Brian Rinaldi is a Developer Experience Engineer at LaunchDarkly with over 20 years experience as a developer for the web. Brian is active in the community running CFE.dev and Orlando Devs.
Thanks for the comment. I had received similar recommendations. A team that I worked with even had ESLint set to enforce using const for every variable where it wasn't reassigned - even objects that were mutated. This is where it caused me confusion and it was probably more common than actual primitive constants in the code. As a personal style preference, I would not choose to do that where I have the option.
When seeing let, my first thought is why that's not const? Using let communicates: watch out, we are doing some weird stuff here. Usually it signals programming paradigm change.
For example, in react app, you need maybe one or two let variables for some special cases. It steps out quite bit in the middle of declarative functional code.
The difference between consts which preserve value and consts which preserve references, reflects the difference between variables that get passed by value and those which get passed by reference.
When a variable holding a primitive value gets passed to a function, the value gets copied into function's parameters ("pass by value"). The function can make changes to the value without affecting the variable's value outside the function. This holds for variables declared with var, const or let. I see a symmetry between the value's immutability, when declared with const, and the pass-by-value rules.
var bar = 'bar'
const baz = 'baz'
let qux = 'qux'
function makeFoo(fromParam) {
fromParam = 'foo'
return fromParam
}
makeFoo(bar) // "foo"
bar // "bar"
makeFoo(baz) // "foo"
baz // "baz"
makeFoo(qux) // "foo"
qux // "qux"
When a variable holding a complex value (variations of Object) gets passed to a function, the function cannot change the reference (pass by reference), but it can change properties of the passed object. This reflects the mutability of complex consts.
I found this observation helped me cement my knowledge about which aspects of variables const protects.
I think objects are referential (like a pointer in other languages) so the const is the reference to that object. At least that is what I tell myself so I can sleep at night 🤣
Yes, objects are passed by reference, but there are no pointers in JavaScript. We can't have direct access to memory addresses like in C language.
I wrote a mini-post describing how references in JS work with objects. By walking through a simple example, it shows that a variable can't reference another variable.
It is not important if language provides access to memory or not. Pointer is always address to some space occupied by data regardless of programming language.
If you will have one object and this object you will put into 500 arrays, you still have only one object on the same address (on the same pointer).
So we can say that even javascript has pointers/addresses.
I agree that under the hood there are pointers, memory allocation, e.t.c. I mean that JavaScript doesn't have syntax to work pointers directly and allocate/free memory like it's done in C or similar languages.
quick search says that for Java final is also just about referencing(for immutability there is no modifier). say for C++ it's combined with sealing(but needs you to declare "change safe" methods as const too).
I am not confused by JS consts behaviour. It acts like it should be. The same way as most of programming languages. Actually I have never worked with language that behave differently.
I could recommend that you use typescript instead. If you use readonly modifiers and const assertion you can have immutable code in development, and better performance in production.
We're a place where coders share, stay up-to-date and grow their careers.
Other than the readability aspect (which is indeed big), there are a few other bnefits to using
constthat many people don't think about:conston the left side of an assignment expression, even if the assignment would be to a property of the referenced object.Honestly, if I know for certain that something shouldn't change, I use const, period. Yes, it can lead to confusion, but that confusion is usually less significant than properly annotating that something shouldn't change.
You should check out Object.freeze if you want
constto be immutable or Object.seal if you wantconstto just be a little bit mutable.Thanks. Yes, I didn't bring them up because I thought it might sidetrack the discussion of
consta bit but if immutability really matters for what you are doing, then definitely.I had always assumed they were immutable, was shocked to find they weren't.
The immutability was the key reason I started to use them.
The issue here is that there are 2 similar but different concepts: referential transparency (
const) vs immutability (Object.freezeor even better recursiveObject.freeze)In practice, I do what Wes Bos had recommended in his ES6 course: use
constfor every single variable, and only change toletif I will need to change that variable's assignment.Thanks for the comment. I had received similar recommendations. A team that I worked with even had ESLint set to enforce using
constfor every variable where it wasn't reassigned - even objects that were mutated. This is where it caused me confusion and it was probably more common than actual primitive constants in the code. As a personal style preference, I would not choose to do that where I have the option.When seeing
let, my first thought is why that's notconst? Usingletcommunicates: watch out, we are doing some weird stuff here. Usually it signals programming paradigm change.For example, in react app, you need maybe one or two
letvariables for some special cases. It steps out quite bit in the middle of declarative functional code.The difference between
consts which preserve value andconsts which preserve references, reflects the difference between variables that get passed by value and those which get passed by reference.When a variable holding a primitive value gets passed to a function, the value gets copied into function's parameters ("pass by value"). The function can make changes to the value without affecting the variable's value outside the function. This holds for variables declared with
var,constorlet. I see a symmetry between the value's immutability, when declared withconst, and the pass-by-value rules.When a variable holding a complex value (variations of Object) gets passed to a function, the function cannot change the reference (pass by reference), but it can change properties of the passed object. This reflects the mutability of complex
consts.I found this observation helped me cement my knowledge about which aspects of variables
constprotects.I think objects are referential (like a pointer in other languages) so the const is the reference to that object. At least that is what I tell myself so I can sleep at night 🤣
Yes, objects are passed by reference, but there are no pointers in JavaScript. We can't have direct access to memory addresses like in C language.
I wrote a mini-post describing how references in JS work with objects. By walking through a simple example, it shows that a variable can't reference another variable.
References in JavaScript
Eugene Karataev ・ May 7 ・ 1 min read
It is not important if language provides access to memory or not. Pointer is always address to some space occupied by data regardless of programming language.
If you will have one object and this object you will put into 500 arrays, you still have only one object on the same address (on the same pointer).
So we can say that even javascript has pointers/addresses.
I agree that under the hood there are pointers, memory allocation, e.t.c. I mean that JavaScript doesn't have syntax to work pointers directly and allocate/free memory like it's done in C or similar languages.
quick search says that for Java
finalis also just about referencing(for immutability there is no modifier). say for C++ it's combined with sealing(but needs you to declare "change safe" methods asconsttoo).I am not confused by JS consts behaviour. It acts like it should be. The same way as most of programming languages. Actually I have never worked with language that behave differently.
And differences between let and var is about scope: stackoverflow.com/questions/762011...
If you don't know what to use then use let.
Yes! Absolutely! It’s all about immutability.
And if you’re using TypeScript you should consider additionally using the Readonly keyword to really drive it home.
Thoughts on Immutable.js ? I like the idea of using it, but it sometimes makes things more verbose than they need to be
I could recommend that you use typescript instead. If you use readonly modifiers and const assertion you can have immutable code in development, and better performance in production.