# prefer-while

Rule `sonarjs/prefer-while`. A "while" loop should be used instead of a "for" loop

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 8.3M weekly downloads of presets that enable it
- Trend: +77.8% last quarter
- Rank: #1142 by reach, #451 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/prefer-while` | yes | safe | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-1264/) |
| Biome | `complexity/useWhile` | no | safe | group complexity, since 1.0.0 | [docs](https://biomejs.dev/linter/rules/use-while/) |

## Code examples

### Incorrect: the rule reports this

```js
for (;condition;) { /*...*/ }
```

### Correct: the rule accepts this

```js
while (condition) { /*...*/ }
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/prefer-while": "error",
    },
  },
];
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "complexity": {
        "useWhile": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |

## Related rules

- [sonarjs/cognitive-complexity](https://lintradar.dev/rules/sonarjs/cognitive-complexity.md): Cognitive Complexity of functions should not be too high
- [sonarjs/no-nested-template-literals](https://lintradar.dev/rules/sonarjs/no-nested-template-literals.md): Template literals should not be nested
- [sonarjs/anchor-precedence](https://lintradar.dev/rules/sonarjs/anchor-precedence.md): Alternatives in regular expressions should be grouped when used with anchors
- [sonarjs/argument-type](https://lintradar.dev/rules/sonarjs/argument-type.md): Arguments to built-in functions should match documented types
- [sonarjs/arguments-order](https://lintradar.dev/rules/sonarjs/arguments-order.md): Parameters should be passed in the correct order
- [sonarjs/array-callback-without-return](https://lintradar.dev/rules/sonarjs/array-callback-without-return.md): Callbacks of array methods should have return statements

---

Web page: https://lintradar.dev/rules/sonarjs/prefer-while. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
