question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `isArray()` rather than `instanceof Array`

See original GitHub issue

I’d like to propose a new rule: that checking for <foo> instanceof Array and suggesting that it should be rewritten as Array.isArray(<foo>).

Using instanceof Array can return false when checking an array object created by another window (or possibly another process on node?), whereas Array.isArray() would work as expected. A more in-depth discussion can be found here.

What category of rule is this? (place an “X” next to just one item)

[ ] Enforces code style [x] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

if (anArray instanceof Array) {
    // do something...
}
var foo = anArray instanceof Array ? true : false

Why should this rule be included in ESLint (instead of a plugin)?

My assumption is that this issue is core to both javascript in the browser and node.js, so I would assume it should go into ESLint itself. Array.isArray() has been supported in JavaScript since ES5.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
mysticateacommented, Dec 14, 2016

FWIW, I have the rule in my plugin: https://github.com/mysticatea/eslint-plugin#book-usage

1reaction
platinumazurecommented, Dec 13, 2016

@ljharb You’re certainly welcome to lay out a proposal. To be honest, I don’t know how I feel about that at this point.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Difference between using Array.isArray and instanceof Array
According to this jsperf, instanceof Array is faster than Array. ... isArray() performs more robust checking and therefore takes a slight performance hit....
Read more >
array like objects - ESDiscuss.org
Some ways I've seen: (1) input instanceof Array (2) Object.prototype. ... isArray() but more permissive // description: // Doesn't strongly test for ...
Read more >
ECMA-262 Edition 5.1 - ECMAScript Language Specification
15.4.1 The Array Constructor Called as a Function; 15.4.2 The Array ... If more than three levels are required these rules repeat with...
Read more >
Better Array check with Array.isArray | SamanthaMing.com
You can think of frames like different planets. Every planet has its own system, with different gravity pull and composition. So instanceof only...
Read more >
eslint-plugin-unicorn - npm
More than 100 powerful ESLint rules. ... Propose or contribute a new rule ➡ ... isArray() instead of instanceof Array .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found