# prefer-for-of-over-for-each

Prefer for-of over sequential forEach callbacks

A `forEach` callback introduces a function boundary for ordinary sequential iteration.
Instead, you SHOULD use a for-of loop when the callback boundary is unnecessary.

A `return` inside the callback exits only that callback and requires manual restructuring.

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

## Reported

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

## Accepted

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

## Prior art

- [eslint-plugin-unicorn · no-for-each](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-for-each.md)

[language/linter/src/rules/style/prefer_for_of_over_for_each.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/prefer_for_of_over_for_each.rs#L8)
