# prefer-iterable-in-constructor

Rule `unicorn/prefer-iterable-in-constructor`. Prefer passing iterables directly to constructors instead of filling empty collections.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #979 by reach, #1234 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-iterable-in-constructor` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-iterable-in-constructor.md) |

## Code examples

### Incorrect: the rule reports this

```js
const set = new Set();
for (const item of items) {
  set.add(item);
}
```

```js
const map = new Map();
for (const [key, value] of entries) {
  map.set(key, value);
}
```

```js
const searchParameters = new URLSearchParams();
for (const [key, value] of Object.entries(record)) {
  searchParameters.set(key, value);
}
```

### Correct: the rule accepts this

```js
const set = new Set(items);
```

```js
const map = new Map(entries);
```

```js
const searchParameters = new URLSearchParams(record);
```

Examples from the ESLint documentation.

## Enable it

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

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-iterable-in-constructor": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-identifier-import-export-specifiers](https://lintradar.dev/rules/unicorn/prefer-identifier-import-export-specifiers.md): Prefer identifiers over string literals in import and export specifiers.
- [unicorn/prefer-iterator-helpers](https://lintradar.dev/rules/unicorn/prefer-iterator-helpers.md): Prefer iterator helpers over temporary arrays from iterators.
- [unicorn/prefer-has-check](https://lintradar.dev/rules/unicorn/prefer-has-check.md): Prefer `.has()` when checking existence.
- [unicorn/prefer-iterator-to-array-at-end](https://lintradar.dev/rules/unicorn/prefer-iterator-to-array-at-end.md): Prefer moving `.toArray()` to the end of iterator helper chains.
- [unicorn/prefer-global-number-constants](https://lintradar.dev/rules/unicorn/prefer-global-number-constants.md): Prefer global numeric constants over `Number` static properties.
- [unicorn/prefer-literal-ascii](https://lintradar.dev/rules/unicorn/prefer-literal-ascii.md): Prefer literal printable ASCII characters over escape sequences.

---

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