Skip to content

instanceOfArrays

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

✅ This rule is included in the ts logical and logicalStrict 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 (
const value: any
value
instanceof
var Array: ArrayConstructor
Array
) {
const value: any[]
value
.
Array<any>.forEach(callbackfn: (value: any, index: number, array: any[]) => void, thisArg?: any): void

Performs the specified action for each element in an array.

@paramcallbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

forEach
(
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(...data: any[]): void (+1 overload)

The console.log() static method outputs a message to the console.

MDN Reference

log
);
}
const
const isArray: boolean
isArray
=
const data: any
data
instanceof
var Array: ArrayConstructor
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 ❤️‍🔥 around the world by the Flint team and contributors.