There is a simple reason for the prevalence of for(let i
instead of for(const i
among even experienced devs/tutorial makers who are not modifying the value in the loop body and understand const/let.
Many people see const as a 'constant' which should never change. ever. (of course it never changes). But they further feel awkward 'redefining multiple constants with the same name'. For example, having const server = 'fun.example.com'
in one place, and const server = 'boring.example.com'
in another place would be 'objectionable'.
The variables in loops (at least in most C-like syntax languages which JavaScript is based on) are the variables that change THE MOST. Nothing gets it's value changed more than the 'i' in all the for loops. The 'i' typically ends up being a 'register variable' on the CPU itself (not in RAM) so that it can 'change faster'. And this was even true in JavaScript since it's inception with 'var', and is still true when you do a simple for(let i=0;i<50;i++)
. ie for(const i=0;i<50;i++)
throws an error.
So you can start to see the dissonance between an 'i' that changes (or is redefined) potentially thousands of times per second as you iterate through a list and for(const i
. So for(const i...
'looks' or 'feels' like i
will only ever have the first value it is assigned. It seems 'wrong' to say const i..
and on the next line of code, i
could be totally different values each time 'through the loop'. And even if you 'know' you are 'redefining it for each run through the loop', to some devs, that itself is questionable.
For this reason, many devs prefer to reserve const
in JavaScript for 'values that are defined once and represent a single value throughout the 'whole execution of the program' or at least 'everything in a class'.
So the answer to the question in your 'title' (should I use) is probably 'keep using const since you have a reason and it makes sense to you'. And the answer to the doubt you had of "why is for(let i so common" is what I answered here as well. For some, they just have habit of for(let
because it works for both 'for loops' and 'iterations through list'. For others, it's a conscious choice because they don't like 'redefining a const' over and over.
And I would humbly suggest that if you do use const
please don't use i
for the 'item' since i is so associated with an integer variable that increments. At least if you use a good name like item it feels more comfortable thinking of it as a single thing that only existed once. And I humbly suggest that if you use 'let' also don't use let i of
for the same reason. Use i
only for actual incrementing items. Ie use for(let item of items) or for(let i=0;i<y;i++)
const
vslet
, the fact that they're declared in afor..of
loop declaration doesn't change anything – Teddifor...of
, I know whatconst
means in general. If what you say is true, why are developpers ever using let? I see them using const everywhere else, but still let in loops. – Lanyardnumber
later? – Tragedianlet
because they're either too lazy to write additional characters, or because they don't care about the difference. Unless it actually gets reassigned somewhere, feel fee to just call it bad code and useconst
instead (IMO) – Teddifor(let x...
in examples, so I was thinking there must be something other developpers know and I don't. – Lanyardfor
loop declaration makes no difference at all – Teddinumber
insidefor...of
for whatever reason, uselet
(Something likeanotherArray.push(++number)
. Else, useconst
. – DoucetteEDIT:
section in the text was not appropriate so I rolled it back. – Lanyard