Use for...of Loop

for...of loops over iterable objects, for example arrays or strings.

It is easier to read than indexed for loops and can replace them in many cases.

For example,

for (let i = 0; i < elements.length; i++) {
    const element = elements[i];
    console.log(element);
}

can be replaced by

for (const element of elements) {
    console.log(element);
}

Learn More: for...of (MDN)

Original Code

Changes

1const n = elements.length;
2for (const i = 0; i < n; ++i) {
3 const element = elements[i];
1for (const element of elements) {
42 console.log(element);
53}

Transformed Code

for (const element of elements) {
    console.log(element);
}

Explore More