As I slowly make my way into the land of ES6 (sure I started mid-2016 when all the cool kids were doing it for years) I’ve been presented with the problem of: when do you use const
and when do you use let
and do I still use var
.
Context: node.js
TL,DR: always use const
, except for primitives whose value will change, then use let
.
[](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
[READER DISCOUNTSave $50 on terminal.training](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
[I’ve published 38 videos for new developers, designers, UX, UI, product owners and anyone who needs to conquer the command line today.](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
🚮 var: no more[](#-var-no-more)
Don’t use var
at all. Which sucks, because I’ve got over 17 years of muscle memory writing var
.
Which is sort of sad because now I can’t do any magic [hoisting](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/var#var_hoisting). Which, in fact is almost enough by itself to stop using var
. However, we also now have const
and let
to define variables and var
really isn’t needed.
Honestly, I can’t see the need for this (again, context is Node) at all anymore…maybe I’m wrong?
💎 const: always, but not always[](#-const-always-but-not-always)
The first important thing to always keep in mind when using const
is that it’s not really really constant. Or more specifically, it doesn’t make objects immutable.
The best way (I’ve found) to think of const
is that the variable that you assign with const
is an immutable pointer. Primitive types, such as strings, booleans and numbers can’t be changed - since the variable is pointing directly at the primitive (so you can’t do [const a = 10; a++
](http://jsconsole.com/?const%20a%20%3D%2010%3B%20a%2B%2B)). Objects and arrays on the other hand, can have their properties changed.
However, it’s been argued by many that using const
is a way to document your intent with a variable to a future reader of the code. My thoughts: always use const
.
☔️ let: the exception[](#-let-the-exception)
Assuming you (or I) only use const
, the only exception is when I know that the variable’s contents will change. For instance, in a for
loop, or some dynamic allocation. Then, and only then, do I need to use let
.
Filing this post under "stuff I shoulda learnt 18 months ago"
Published 9-Aug 2016 under #web. [Edit this post](https://github.com/remy/remysharp.com/blob/main/public/blog/var-const-let.md)
Comments
Lock Thread
Login
Add Comment[M ↓ Markdown]()
[Upvotes]()[Newest]()[Oldest]()

Garibaldi Pentito
0 points
7 years ago
I disagree with the "always use const" advice. I think the opposite: const is confusing
const dt = new Date(); const dad = { name: 'homer' }; dt.setMonth(2); // no error dad.name = 'peter'; // no error
Even more confusingly\ `\`// it forces you to do this for variables that are set only once in your program\ const FOO = config.debug ? 123 : 456;\ const BAR = config.debug ? 123 : 456;
if (config.debug) {\ const FOO = …\ const BAR = …\ } else {\ const FOO = …\ const BAR = …\ }
let FOO, BAR;\ if (config.debug) {\ FOO = …\ BAR = ..\ } else {\ FOO = …\ BAR = ..\ }\ `\`
I use const just for primitive, old fashioned constants like FIRST\_DAY\_OF\_WEEK or LANG, let for everything else. Never had a problem.

rem
0 points
7 years ago
…and this is exactly why I said that you need to consider the fact that a variable pointing to an object is simply a pointer to memory. Using let
for an object, does also mean that the variable could be switched from an object to a string without any error thrown in your code.
Bottom line though: JavaScript is extremely malleable—so use it the way it works for you.

Garibaldi Pentito
0 points
7 years ago
Of course we use what works for us, we are here to compare notes on "best practices" since people are still debating the subject and will use blogs by established devs like yourself to back their point.
I am saying in practice having a const pointing to memory is of limited use, all it prevents you from doing is assigning a string to an object, say, but you can still completely change the object with another. So the actual practical benefits of having "const" as default are very limited.\
const dad = { name: 'homer' }; dad = { method: 'post' }; // error delete dad.name; dad.method = 'post'; // same result, but this time no error

rem
0 points
7 years ago
I don’t agree that this is bad. I used to. But I don’t anymore, having used it more.
The reason I wasn’t keen is that const
didn’t align with me expectation of what const is (from other languages). But JavaScript isn’t other languages. The same was as there’s no such thing as class inheritance in JavaScript and many have tried to implement it, but until you start thinking of how JavaScript really works through the prototype chain and delegation, then the developer is going to be trying to force incompatible patterns.
To me, the fact that the const refers to a point that can’t be changed is important. The fact the properties can change is due to the object not being frozen. This should be intuitive if you understand JavaScript. But if, say, you come from Java, then yeah, it’s going to "wrong" that const doesn’t freeze the entire object.
But like I said, JavaScript doesn’t work like that.

Garibaldi Pentito
0 points
7 years ago
Well, freezing an object only works on the properties that are primitives, so there is no native way of creating REAL constant objects (it’s the same in Java btw.) But as you say it’s a matter of personal preference.

Alex Ilyaev
0 points
7 years ago
Seeing this post being referenced from different places, I’d expect the post to talk about the actual difference between var and the other 2 in terms of how they behave:
-
let and const are block scoped (with example)
-
let and const do not hoist but they do set the variable at the block scope, so calling them before they’re defined will fail, but also cut the reference to an outer variable with the same name
-
etc.
And as a personal question, do you think we’d have performance improvement when using const vs let in the future?

jason frazzano
0 points
7 years ago
As a result they don’t cause a state to remain manifest on an object, or component for any knowable, measurable, reasonably recordable amount of time. Meaning, no state reporting is possible unless you go out of your way to broadcast your state to the environment. That small little difference can have security benefits, especially when dealing with corporate clients.

Paweł Grzybek
0 points
7 years ago
Great article Remy. Kind of same conclusions as Mathias' [https://mathiasbynens.be/no…;](https://mathiasbynens.be/notes/es6-const)

Roger Kondrat
0 points
7 years ago
@rem I wish you would take a few minutes to add an additional example to const and 2 to let, because as a newb examples are king and words are well just words. Still a really helpful article, I just wish it had some extras to help my audience type.
Please and thank you!

Julian Nicholls
0 points
7 years ago
This is more or less the conclusion that I came to when writing code for Node, React etc

Jakobud
0 points
7 years ago
I’ve been wondering this myself. Thanks,
[Commento](https://commento.io)