# prefer-number-is-safe-integer

Rule `unicorn/prefer-number-is-safe-integer`. Prefer `Number.isSafeInteger()` over integer checks.

- 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: #1072 by reach, #1327 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
if (!Number.isInteger(index)) {
  throw new Error('Expected an integer.');
}
```

```js
// Different integer literals can round to the same number.
Number.isInteger(largeNumber); // true (misleading!)
largeNumber === 9007199254740993; // true (precision lost!)
```

```js
function processId(id) {
  if (!Number.isInteger(id)) {
    throw new Error('Invalid ID');
  }
  // id could still be too large to represent exactly
}
```

### Correct: the rule accepts this

```js
if (!Number.isSafeInteger(index)) {
  throw new Error('Expected a safe integer.');
}
```

```js
Number.isSafeInteger(largeNumber); // false (correctly identifies the issue)
```

```js
function processId(id) {
  if (!Number.isSafeInteger(id)) {
    throw new Error('Invalid ID');
  }
  // id is guaranteed to be safely representable
}
```

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-number-is-safe-integer": "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% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-location-assign](https://lintradar.dev/rules/unicorn/prefer-location-assign.md): Prefer `location.assign()` over assigning to `location.href`.
- [unicorn/prefer-object-destructuring-defaults](https://lintradar.dev/rules/unicorn/prefer-object-destructuring-defaults.md): Prefer object destructuring defaults over default object literals with spread.
- [unicorn/prefer-iterator-to-array](https://lintradar.dev/rules/unicorn/prefer-iterator-to-array.md): Prefer `Iterator#toArray()` over temporary arrays from iterator spreads.
- [unicorn/prefer-observer-apis](https://lintradar.dev/rules/unicorn/prefer-observer-apis.md): Prefer observer APIs over resize and scroll listeners with layout reads.
- [unicorn/prefer-includes-over-repeated-comparisons](https://lintradar.dev/rules/unicorn/prefer-includes-over-repeated-comparisons.md): Prefer `.includes()` over repeated equality comparisons.
- [unicorn/prefer-options-parameter](https://lintradar.dev/rules/unicorn/prefer-options-parameter.md): Prefer an options object over optional positional parameters.

---

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