JavaScript const vs let Debunked
Mastering Variables in JavaScript
const
and let
are two keywords used to declare variables in JavaScript.
Recently, there has been a debate on which keyword is better, which I believe to be absurd.
In this article, I will explain the key differences between const
and let
, then give my take on how both keywords should be used in JavaScript.
So without further ado, lets dive right in!
This debate originates from the “let me be” talk by Ryan Florence.
Ryan makes controversial remarks about the use of const
and let
, and shows a clear subjective bias towards the use of let
.
I will not cover this in the article, since Jack Herrington evaluates this peculiar talk logically and objectively.
This article instead covers the fundamental differences between let
and const
.
This will enable you to make better choices based on the context, instead of following baseless and subjective opinions.
2. The Differences Between let and const
2.1 Re-Assignment
The key difference between let
and const
lies in the idea of re-assignment.
With let
, you can re-assign a variable, meaning you can replace the actual reference link between the variable name to its value.
This is not possible using const
.
Lets define two variables using let
and const
to see what I mean:
Now lets draw the reference links for both variables:
Therefore, a statement like x = 2
is possible, because the reference link will change and point to 2 instead of 1.
The equivalent statement y = 2
, doesn’t work since the reference link is locked in place.
2.2 Mutability
While const
works as expected for primitive types, e.g. number
, the same can’t be said for more complex types such as arrays or objects.
Lets look at the example below:
Even though this variable is marked as const
, you can still perform array operations such as push
.
This is counter intuitive as const
should denote a “constant”, something unassignable and immutable after definition.
But in JavaScript, the idea of mutability is independent from re-assignment.
For example, the following code works using let
but not using const
:
2.3 True Immutability in JavaScript
In JavaScript there are only two ways to achieve true constants:
- Using
const
with primitive types. - Using
Object.freeze
or an immutability library.
Using our previous example, we can make the array immutable using Object.freeze
:
This, however, is known as shallow freezing, which means only the first level is immutable, while the nested levels are still mutable:
The only way to get true immutability for objects with nested properties is by using immutability libraries like immutable-js.
This library contains no dependencies, and provides an array of different structures that are immutable and behave as expected.
3. My Take
After reading this article, you should be experts with let
and const
.
You can now confidently decide which keyword to use in any context.
Nevertheless, my opinion is to utilize let
for situations when you need selective/iterative re-assignment, e.g. flag variables, loop counters.
I believe const
should be used everywhere else to promote consistency.
If you enjoyed this article, please make sure to Subscribe, Clap, Comment and Connect with me today! 🌐