# no-element-overwrite

Rule `sonarjs/no-element-overwrite`. Collection elements should not be replaced unconditionally

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

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
fruits[1] = "banana";
fruits[1] = "apple";  // Noncompliant - value on index 1 is overwritten

myMap.set("key", 1);
myMap.set("key", 2); // Noncompliant - value for key "key" is replaced

mySet.add(1);
mySet.add(1); // Noncompliant - element is already in the set
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/no-element-overwrite": "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/no-duplicated-branches](https://lintradar.dev/rules/sonarjs/no-duplicated-branches.md): Two branches in a conditional structure should not have exactly the same implementation
- [sonarjs/no-empty-after-reluctant](https://lintradar.dev/rules/sonarjs/no-empty-after-reluctant.md): Reluctant quantifiers in regular expressions should be followed by an expression that can't match the empty string
- [sonarjs/no-duplicate-test-title](https://lintradar.dev/rules/sonarjs/no-duplicate-test-title.md): Test titles should be unique within the same suite
- [sonarjs/no-empty-alternatives](https://lintradar.dev/rules/sonarjs/no-empty-alternatives.md): Alternation in regular expressions should not contain empty alternatives
- [sonarjs/no-duplicate-parameterized-test-case](https://lintradar.dev/rules/sonarjs/no-duplicate-parameterized-test-case.md): Parameterized tests should not contain duplicate test cases
- [sonarjs/no-empty-character-class](https://lintradar.dev/rules/sonarjs/no-empty-character-class.md): Empty character classes should not be used

---

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