Skip to content

instanceOfArrays

Reports using instanceof Array instead of Array.isArray().

✅ This rule is included in the ts logical presets.

Using instanceof Array to check if a value is an array has two potential issues:

  1. Cross-realm failures: In JavaScript, each execution context (such as an iframe or web worker) has its own global scope with its own Array constructor. When arrays are passed between contexts, instanceof Array returns false because the array was created with a different Array constructor.

  2. Array-like objects: Some objects like arguments have array-like behavior but are not instances of Array.

The Array.isArray() method reliably detects arrays regardless of which realm they were created in.

if (value instanceof Array) {
value.forEach(console.log);
}
const isArray = data instanceof Array;

This rule is not configurable.

If you are certain that your code will never receive arrays from other execution contexts (such as iframes) and you don’t need to handle array-like objects, you might not need this rule. Some projects with those characteristics prefer to stick with instanceof for consistency with other class instance checks.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.