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.

no-for-loop should detect and ignore variables populated with DOM node if possible

See original GitHub issue

For loops for non-iterable DOM nodes are treated as iterable arrays.

I’m not sure if this is detectable, surely at least some edge cases wouldn’t be - but when the variable is populated immediately above the loop it seems at least theoretically feasible.

Affected rule: no-for-loop

Input:

const visibleItems = document.querySelector('.visible');
for (let x = 0; x < visibleItems.length; x++) {
  someFunc(visibleItems[x])
}

Output:

const visibleItems = document.querySelector('.visible');
for (const [x, visibleItem] of visibleItems.entries()) {
  someFunc(visibleItem)
}

As an aside, the fact that the auto-fix depluralizes visibleItems[x] to visibleItem is a very nice touch!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
sindresorhuscommented, Sep 19, 2021

Your code is invalid. querySelector is not iterable because it returns a single element. I think you meant to use querySelectorAll which is actually iterable.

1reaction
fregantecommented, Sep 20, 2021

In short, no-for-loop breaks on array-like objects.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5. Working with Arrays and Loops - JavaScript Cookbook [Book]
One you’ve created an array, using Array object or literal notation, you can access the array elements in a loop, or use any...
Read more >
Master the art of looping in JavaScript with these incredible tricks
In Do While loop the condition to be checked is given at the end, and so the loop executes at least once even...
Read more >
Traversing an HTML table with JavaScript and DOM Interfaces
This article is an overview of some powerful, fundamental DOM level 1 methods and how to use them from JavaScript. You will learn...
Read more >
The 10 Most Common JavaScript Issues Developers Face
If you need help figuring out why your JavaScript isn't working, consult this list of the 10 most common JavaScript issues from a...
Read more >
How do I iterate through table rows and cells in JavaScript?
Furthermore, it gives your code access to both element and index vars rather than just the element, and if you prefer to hide...
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