Rule proposal: `isArray()` rather than `instanceof Array`
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:5
- Comments:8 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
FWIW, I have the rule in my plugin: https://github.com/mysticatea/eslint-plugin#book-usage
@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.