DEV Community

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

let vs const - Let's discuss.

Kumar Abhirup
I am a 16-year-old JavaScript React developer from India who keeps learning a new thing every single day. 😍 Twitter: @kumar_abhirup Website: kumar.now.sh
・2 min read

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!

Discussion (17)

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
kumar_abhirup profile image
Collapse
mudlabs profile image
Sam

CONSTANT FOR ALL team member checking in.

Collapse
kumar_abhirup profile image
Kumar Abhirup Author

Nice. I got a teammate. ⚑

Collapse
armw4 profile image
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
kumar_abhirup profile image
Collapse
armw4 profile image
Antwan R. Wimberly

Facts‼️

Collapse
sergchr profile image
Sergiy

Would you write about arrow functions?

Collapse
kumar_abhirup profile image
Kumar Abhirup Author

Yes! What would you like to know from me?

Collapse
sergchr profile image
Sergiy

How to use them in general

Thread Thread
kumar_abhirup profile image
Kumar Abhirup Author

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
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
kumar_abhirup profile image
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
armw4 profile image
Antwan R. Wimberly

Sheesh πŸ˜’

Forem Open with the Forem app