useForOf (since v1.5.0)
Diagnostic Category: lint/nursery/useForOf
Source: prefer-for-of
This rule recommends a for-of
loop when in a for
loop, the index used to extract an item from the iterated array.
Examples
Section titled ExamplesInvalid
Section titled Invalidfor (let i = 0; i < array.length; i++) { console.log(array[i]);}
nursery/useForOf.js:1:1 lint/nursery/useForOf ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Use for-of loop instead of a for loop.
> 1 │ for (let i = 0; i < array.length; i++) {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ console.log(array[i]);
> 3 │ }
│ ^
4 │
Valid
Section titled Validfor (let i = 0; i < array.length; i++) { console.log(i, array[i]); }
for (let i = 0, j = 0; i < array.length; i++) { console.log(i, array[i]); }