# updated-loop-counter

Rule `sonarjs/updated-loop-counter`. Loop counters should not be assigned within the loop body

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1382 by reach, #683 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/updated-loop-counter` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-2310/) |

## Code examples

### Incorrect: the rule reports this

```js
var names = [ "Jack", "Jim", "", "John" ];
for (var i = 0; i < names.length; i++) {
  if (!names[i]) {
    i = names.length;                                 // Non-Compliant
  } else {
    console.log(names[i]);
  }
}
```

### Correct: the rule accepts this

```js
var names = [ "Jack", "Jim", "", "John" ];
for (var name of names) {
  if (!name) {
    break;                                 // Compliant
  } else {
    console.log(name);
  }
}
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/updated-loop-counter": "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% |

## Related rules

- [sonarjs/updated-const-var](https://lintradar.dev/rules/sonarjs/updated-const-var.md): "const" variables should not be reassigned
- [sonarjs/use-type-alias](https://lintradar.dev/rules/sonarjs/use-type-alias.md): Type aliases should be used
- [sonarjs/unverified-hostname](https://lintradar.dev/rules/sonarjs/unverified-hostname.md): Server hostnames should be verified during SSL/TLS connections
- [sonarjs/vitest-mock-at-module-scope](https://lintradar.dev/rules/sonarjs/vitest-mock-at-module-scope.md): Vi.mock should be declared at module scope
- [sonarjs/unverified-certificate](https://lintradar.dev/rules/sonarjs/unverified-certificate.md): Server certificates should be verified during SSL/TLS connections
- [sonarjs/void-use](https://lintradar.dev/rules/sonarjs/void-use.md): "void" should not be used

---

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