We all are familiar with the difference between const, let and var. If not, please read this.
Var, let and const- what's the difference?
Sarah Chima ใป Oct 25 '17 ใป 5 min read
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.
TC39 members: const was a mistake.
Reddit: NO DAN YOU'RE WRONG YOU JUST DONT UNDERSTAND JAVASCRIPTS12:53 PM - 21 Dec 2019
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)
constant
for all. It's useful information to the developer about the future of the value (that it won't change). It follows that seeinglet
guarantees that the value will change because it is necessary.The additional information can be helpful.
Agreed.
CONSTANT FOR ALL team member checking in.
Nice. I got a teammate. โก
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.
I like that.
Would you write about arrow functions?
Yes! What would you like to know from me?
How to use them in general
DM me @kumar_abhirup on Twitter about this :)
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 withlet
. So, constant for all.I don't understand the hype of "arrow function" and "const" guy checks in. I understand the need but not the hype.
const for all. Though I do agree to Dan's point that it can be confusing for beginner when you're dealing with objects.
๐ฅ