Skip to content

unicorn/prefer-includes Style

🚧 An auto-fix is still under development.

What it does

Prefer includes() over indexOf() when checking for existence or non-existence. All built-ins have .includes() in addition to .indexOf().

Why is this bad?

The .includes() method is more readable and less error-prone than .indexOf().

Examples

Examples of incorrect code for this rule:

javascript
if (str.indexOf("foo") !== -1) {
}

Examples of correct code for this rule:

javascript
if (str.includes("foo")) {
}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-includes
json
{
  "rules": {
    "unicorn/prefer-includes": "error"
  }
}

References

Released under the MIT License.