DEV Community

Cover image for let vs const - Let's discuss.
Kumar Abhirup
Kumar Abhirup

Posted on

let vs const - Let's discuss.

We all are familiar with the difference between const, let and var. If not, please read this.


For those who are familiar, should know that in the modern-day JavaScript, YOU SHOULD NEVER USE var.

So now, what we are left with, is the let and const.

๐Ÿ”ฅ The two scenarios

People believe their ways of using them both. Strongly.

Here are the two types of people.

1) Those who use const for Constants (Like for const PI = 3.14)
2) Those who use const for everything that won't be let

๐Ÿ“ฏ const for Constants

Some people believe that const should only be used for strictly constant values like the Action Type Reducer Strings, Math values and constants like PI, etc.

If you are that person, you are from team CONSTANT SPARINGLY.

๐Ÿ“ฏ const for everything that won't be let

If you always use const, no matter what, and only use let when you change a variable, you are from team CONSTANT FOR ALL.


There has been a lot of talk around it on Twitter due to this tweet by Dan Abramov.

The tweet pretty much sums up that he is from the team CONSTANT SPARINGLY.

If you have been seeing WesBos' tutorials, he seems like he is from the team CONSTANT FOR ALL.

Dan has provided a beautiful explanation for why he thinks const shouldn't be used.

Also, this writeup here focuses on easily concluding this discussion. But still, what's your opinion on it?


What do you prefer? Let's Discuss!

Top comments (14)

Collapse
 
sargalias profile image
Spyros Argalias

constant for all. It's useful information to the developer about the future of the value (that it won't change). It follows that seeing let guarantees that the value will change because it is necessary.

The additional information can be helpful.

Collapse
 
kumareth profile image
Kumar Abhirup

Agreed.

Collapse
 
mudlabs profile image
Sam

CONSTANT FOR ALL team member checking in.

Collapse
 
kumareth profile image
Kumar Abhirup

Nice. I got a teammate. โšก

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I believe, the best should be CONSTANT FOR ALL, and not only constant for all, but also IMMUTABILITY. That is, avoid mutation, whether instance or memory address.

Collapse
 
kumareth profile image
Kumar Abhirup

I like that.

Collapse
 
sergchr profile image
Serhii

Would you write about arrow functions?

Collapse
 
kumareth profile image
Kumar Abhirup

Yes! What would you like to know from me?

Collapse
 
sergchr profile image
Serhii

How to use them in general

Thread Thread
 
kumareth profile image
Kumar Abhirup

DM me @kumar_abhirup on Twitter about this :)

Collapse
 
apoorvsingal profile image
Apoorv Singal

Well, variables delcared using const keyword are allocated on stack instead of the heap (unless you're using closures), making them way faster than the variables declared with let. So, constant for all.

Collapse
 
iamkalai profile image
Kalaiarasan Pushpanathan

I don't understand the hype of "arrow function" and "const" guy checks in. I understand the need but not the hype.

Collapse
 
saurabhdaware profile image
Saurabh Daware ๐ŸŒป

const for all. Though I do agree to Dan's point that it can be confusing for beginner when you're dealing with objects.

Collapse
 
kumareth profile image
Kumar Abhirup

๐Ÿ”ฅ