# prefer-for-of

Prefer for-of when a loop only indexes one array

A complete index loop whose counter only selects array elements maintains an index it never uses directly.
Instead, you SHOULD iterate over the array values with a for-of loop.

- Category: style
- Level: warning
- Fix: none
- Scope: module

## Reported

```ds title="main.ds"
function copy(values: int32[], output: int32[]): void {
    for (let index: isize = 0; index < values.length; index++) {
        output.push(values[index]);
    }
}
```

## Accepted

```ds title="main.ds"
function copy(values: int32[], output: int32[]): void {
    for (const value of values) {
        output.push(value);
    }
}
```

## Prior art

- [typescript-eslint · prefer-for-of](https://typescript-eslint.io/rules/prefer-for-of)

[language/linter/src/rules/style/prefer_for_of.rs:7](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/prefer_for_of.rs#L7)
