ESLint InterlaceESLint Interlace
Plugin: conventionsRules

consistent-existence-index-check

ESLint rule documentation for consistent-existence-index-check

📡 Live from GitHub — This documentation is fetched directly from consistent-existence-index-check.md and cached for 6 hours.

Keywords: indexOf, includes, array, consistency, ESLint rule, auto-fix, LLM-optimized

Enforce consistent style for checking if an element exists in an array

Enforce consistent style for checking if an element exists in an array. This rule is part of eslint-plugin-conventions.

Quick Summary

AspectDetails
SeverityWarning (code quality)
Auto-Fix✅ Yes (converts pattern)
CategoryQuality
ESLint MCP✅ Optimized for ESLint MCP integration
Best ForCode consistency, modern JavaScript practices

Rule Details

Prefer includes() over indexOf() !== -1 for existence checks.

Why This Matters

IssueImpactSolution
📖 Readability!== -1 is less clearUse includes()
🎯 IntentindexOf suggests index neededClear existence check
🔄 ConsistencyMixed patterns in codebaseStandardize

Examples

❌ Incorrect

if (array.indexOf(item) !== -1) { }
if (array.indexOf(item) >= 0) { }
if (array.indexOf(item) > -1) { }
if (string.indexOf(substring) !== -1) { }

✅ Correct

if (array.includes(item)) { }
if (string.includes(substring)) { }

// indexOf is fine when you need the actual index
const index = array.indexOf(item);
if (index !== -1) {
  array.splice(index, 1);  // Using the index
}

Configuration Examples

Basic Usage

{
  rules: {
    'architecture/consistent-existence-index-check': 'warn'
  }
}

Further Reading

Known False Negatives

The following patterns are not detected due to static analysis limitations:

Dynamic Variable References

Why: Static analysis cannot trace values stored in variables or passed through function parameters.

// ❌ NOT DETECTED - Value from variable
const value = externalSource();
processValue(value); // Variable origin not tracked

Mitigation: Implement runtime validation and review code manually. Consider using TypeScript branded types for validated inputs.

Imported Values

Why: When values come from imports, the rule cannot analyze their origin or construction.

// ❌ NOT DETECTED - Value from import
import { getValue } from './helpers';
processValue(getValue()); // Cross-file not tracked

Mitigation: Ensure imported values follow the same constraints. Use TypeScript for type safety.

On this page

No Headings