persona-community-5/.pnpm-store/v3/files/f3/9fe8e1e63409aa23bc94c43f3a3e7efced19e95f618f0e5d95b44b47967aee81a641c1482a09c0adf0a613658e48303ab93a91239a3d363ffb52c7be591f95
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

51 lines
1.2 KiB
Plaintext

---
description: 'Enforce the use of `for-of` loop over the standard `for` loop where possible.'
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/prefer-for-of** for documentation.
Many developers default to writing `for (let i = 0; i < ...` loops to iterate over arrays.
However, in many of those arrays, the loop iterator variable (e.g. `i`) is only used to access the respective element of the array.
In those cases, a `for-of` loop is easier to read and write.
This rule recommends a for-of loop when the loop index is only used to read from an array that is being iterated.
## Examples
<Tabs>
<TabItem value="❌ Incorrect">
```ts
declare const array: string[];
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
```
</TabItem>
<TabItem value="✅ Correct">
```ts
declare const array: string[];
for (const x of array) {
console.log(x);
}
for (let i = 0; i < array.length; i++) {
// i is used, so for-of could not be used.
console.log(i, array[i]);
}
```
</TabItem>
</Tabs>
{/* Intentionally Omitted: When Not To Use It */}