ESLint InterlaceESLint Interlace
Plugin: modernizationRules

no-instanceof-array

ESLint rule documentation for no-instanceof-array

📡 Live from GitHub — This documentation is fetched directly from no-instanceof-array.md and cached for 6 hours.

Keywords: no-instanceof-array, Array.isArray, type checking, cross-realm, iframes, web workers, reliability, ESLint rule, modernization

Prefer Array.isArray() over instanceof Array for reliable type checking across different JavaScript realms (iframes, Web Workers).

Forbid use of instanceof Array in favor of Array.isArray(). This rule is part of eslint-plugin-modernization and ensures robust array detection in environments with multiple execution contexts.

Quick Summary

AspectDetails
SeverityMedium (Reliability)
Auto-Fix❌ No
CategoryModernization
ESLint MCP✅ Optimized for ESLint MCP integration
Best ForAll projects, especially those using iframes
ContextsCross-realm compatibility

Why instanceof Array is Unreliable

Problem: Each JavaScript environment (realm) has its own global objects, including Array. An array created in one iframe will not be an instance of the Array constructor in another iframe or the main window.

Risk: value instanceof Array will return false if value was created in a different realm, even if it is a valid array. This leads to subtle bugs where data processing logic is skipped or errors are thrown because an array wasn't recognized.

Error Message Format

The rule provides LLM-optimized error messages with actionable technical guidance:

⚠️ MODERNIZATION | Instanceof Array detected | MEDIUM
   Fix: Replace "value instanceof Array" with "Array.isArray(value)" for cross-realm compatibility | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

Message Components

ComponentPurposeExample
Language StandardTechnical benchmarkMDN Array.isArray
Issue DescriptionSpecific violationinstanceof Array fails across realms
Severity & ComplianceImpact assessmentMEDIUM
Fix InstructionActionable remediationUse Array.isArray() instead
Technical TruthOfficial referenceArray detection

Rule Details

This rule flags any binary expression using the instanceof operator where the right-hand side is the Array identifier.

Why This Matters

IssueImpactSolution
🖼️ Cross-realmFalse negatives with iframes/workersArray.isArray() works regardless of the realm origin
🛠️ MaintainabilityUnpredictable behavior in large systemsStandardize on the built-in static method
🔒 RobustnessType guards fail silentlyEnforce universal array detection

Configuration

OptionTypeDefaultDescription
allowstring[][]Patterns/Contexts where instanceof Array is allowed

Examples

❌ Incorrect

// Fails for arrays from other realms (e.g., iframes)
if (value instanceof Array) {
  processItems(value);
}

const isArray = (v: any) => v instanceof Array;

✅ Correct

// Works correctly across all realms
if (Array.isArray(value)) {
  processItems(value);
}

const isArray = (v: any) => Array.isArray(v);

Further Reading

On this page

No Headings