![](https://miro.medium.com/v2/resize:fit:700/1*FkxV_uzaIAObzYVBK5VcCQ.png)
Member-only story
Const, let and var, which and when?
Another article on JavaScript variable assignment 👍
Odds are that you’ve seen the “new” variable assignment keywords const
and let
. They’ve been around a while now 😅
Support for them is good now and if you use a transpiler such as babel
, you’ve likely been using them for some time.
For those in camp TL;DR; If in doubt sort priority of use alphabetically, const
before let
, let
before var
. Prioritise read-only
variables within your code. This reduces the risk of accidental reassignments and unintentional value changes. As a bonus, if you use eslint
, you can use the prefer-const
rule to help you out! ⛑
var
Before transpilers and fancier syntax, there was var
. var
was happy go lucky. You could and still can do whatever you want with it.
![](https://miro.medium.com/v2/resize:fit:700/1*1qmznHr2XJ5P2TujDSM5FA.png)
Its relaxed nature can lead to issues though if you’re not familiar with how it behaves in certain scenarios.
The problem with var?
There is nothing wrong with using var
. There are no problems with using var
. However, there are some gotchas with using var
.