コンテンツにスキップ

useForOf

このコンテンツはまだ日本語訳がありません。

biome.json
{
"linter": {
"rules": {
"style": {
"useForOf": "error"
}
}
}
}

Prefer using for...of loops over standard for loops where possible.

This rule recommends using a for...of loop in place of a for loop when the loop index is solely used to read from the iterated array.

When the loop index is declared within the outer scope or used anywhere within the loop body, it is acceptable to use a for loop. (Array.entries() can be used to a similar effect.)

for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
code-block.js:1:1 lint/style/useForOf ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This for loop only uses its index to read from the iterated array.

> 1 │ for (let i = 0; i < array.length; i++) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ console.log(array[i]);
> 3 │ }
^
4 │

A for…of loop expresses that pattern more clearly because it iterates over the array values directly.

Use a for…of loop instead.

for (let item of array) {
console.log(item);
}
for (let i = 0; i < array.length; i++) {
// `i` is used, so for loop is OK
console.log(i, array[i]);
}
for (let i = 0, j = 0; i < array.length; i++) {
console.log(i, array[i]);
}
let i = 0;
for (; i < array.length; i++) {
console.log(array[i]);
}