Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of reassigning constants inside of JavaScript for loops? [duplicate]

Similar to This Question, what are the advantages of using const inside js for loops instead of let and var for values that change each iteration?

I am not a particular fan of this pattern, but I am interested in learning about the advantages of this pattern.

Background

When I write JavaScript in Visual Studio and type for it suggests the following code block:

for (let index = 0; index < array.length; index++) {
    const element = array[index];
                    
}

I personally am not a fan of this pattern as reassigning a value to a const each iteration as it seems to violate the core idea of a constant ( yes, I know the constant technically goes out of scope, but it still feels like a const is being assigned a new value).

I assume there must be many good reasons for this pattern, otherwise why would this stick around as a suggested code block. I have come up with a good reason, but I am still curious about what advantages I am missing.

Reason I came up with

I could see this being useful within a large code block where you either need to ensure no one uses this name or this name is referenced multiple times expecting the same value.

like image 677
allenretz Avatar asked Jan 21 '26 17:01

allenretz


1 Answers

If you're not happy with

for (let index = 0; index < array.length; index++) {
    const element = array[index];
}

then you may as well not be happy with most uses of const, like those in most functions, since the values often vary.

function getFullName(first, last) {
  const fullName = first + ' ' + last;
  return fullName;
}

The purpose of const is to indicate that that particular variable binding, in the scope it's in, will never be reassigned. That's it. In your loop, a new variable environment is created with every iteration, and into each such environment, a const is created. Using let instead here wouldn't make things clearer - on the contrary, using let just because you're inside a loop (or inside a function, like in my example above) would generally indicate a misunderstanding of scope.

const doesn't mean, and no one should take it to imply:

Every time anything anywhere references a variable named <x>, the value will be <y>.

I suggest using const whenever appropriate - that is, whenever a particular variable binding, in the scope that it's in, never gets reassigned. That's all that's needed for it to be valid, that's how the vast majority of professional developers use it, and that's what it's meant to indicate. You don't have to, of course - you can use let instead - but using const will make the code a bit easier to read at a glance when you know the identifier won't get reassigned in the scope it's in.

Avoid var, especially in loops, and especially if there's any chance of any asynchronous code ever being executed - it has unintuitive function scope, not block scope. for (let i = has a new binding for each iteration; for (var i has only a single binding ever - see this famous question. (var also unintuitively creates properties on the global object when on the top level, and will not warn you when you attempt to declare a variable more than once in a given scope, which is usually indicative of a bug. const and let do not have those problems.)

like image 82
CertainPerformance Avatar answered Jan 23 '26 07:01

CertainPerformance