News for hackers. Stuff that matters.
Re:GitHub's Web Component Collection by steve_adams_86 on Friday March 12, 2021 @01:47PM (#26439316)
The key thing to consider is block scoping in JS. Inside of that loop is a different scope, so any `const` defined in there is not accessible or interfering with higher scopes.

In a way it makes good semantic sense if the value shouldn't change within that scope.

As others have mentioned, in JS this is kind of questionable since arrays and objects don't behave as many would expect a constant to behave.

You're free to use either one, really - it largely comes down to preference.

Re:GitHub's Web Component Collection by tzury on Friday March 12, 2021 @02:25PM (#26439796)
the example in my link shows:

    for (const form of document.forms) {
        const formInput = form.querySelector('input')
        const button = form.querySelector('button')
        const state = form.querySelector('.state')
        ...
    }

formInput, button and state are modified in _each_ iteration.
Re:GitHub's Web Component Collection by steve_adams_86 on Friday March 12, 2021 @02:55PM (#26440164)
Yep, and each iteration replaces the previous one. Think of each iteration as its own scope.

If you want to persist a const across iterations, you'd define it in a scope above it. You could also modify a let variable defined a parent scope. Personally I avoid doing that, but it's possible. Anything defined in an iteration though, I'd consider disposable unless it's a reference to and outer scope variable.

Someone correct me if I'm wrong!

Re:GitHub's Web Component Collection by wcarss on Saturday March 13, 2021 @06:16PM (#26451074)
I think you're right! And the value of doing it this way is that you cannot re-assign these names within the scope: they are const on each iteration of the loop.
Re:GitHub's Web Component Collection by no_wizard on Friday March 12, 2021 @03:03PM (#26440245)
I think more correctly, they're re-defined from scratch on every iteration. The previous const is no longer relevant on the next iteration