# index-of-compare-to-positive-number

Rule `sonarjs/index-of-compare-to-positive-number`. "indexOf" checks should not be for positive numbers

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

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/index-of-compare-to-positive-number` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-2692/) |

## Code examples

### Incorrect: the rule reports this

```js
var color = "blue";
var name = "ishmael";

var arr = [color, name];

if (arr.indexOf("blue") > 0) { // Noncompliant
  // ...
}
```

### Correct: the rule accepts this

```js
var color = "blue";
var name = "ishmael";

var arr = [color, name];

if (arr.indexOf("blue") >= 0) {
  // ...
}
if (arr.includes("blue")) {
  // ...
}
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/index-of-compare-to-positive-number": "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/inconsistent-function-call](https://lintradar.dev/rules/sonarjs/inconsistent-function-call.md): Functions should be called consistently with or without "new"
- [sonarjs/insecure-cookie](https://lintradar.dev/rules/sonarjs/insecure-cookie.md): Cookies should have the "secure" flag
- [sonarjs/in-operator-type-error](https://lintradar.dev/rules/sonarjs/in-operator-type-error.md): "in" should not be used with primitive types
- [sonarjs/insecure-jwt-token](https://lintradar.dev/rules/sonarjs/insecure-jwt-token.md): JWT should be signed and verified with strong cipher algorithms
- [sonarjs/hooks-before-test-cases](https://lintradar.dev/rules/sonarjs/hooks-before-test-cases.md): Lifecycle hooks should not be interleaved with test cases or nested suites
- [sonarjs/inverted-assertion-arguments](https://lintradar.dev/rules/sonarjs/inverted-assertion-arguments.md): Assertion arguments should be passed in the correct order

---

Web page: https://lintradar.dev/rules/sonarjs/index-of-compare-to-positive-number. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
