Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ES6: Iterating over a NodeList
var elements = document.querySelectorAll("div"),
callback = (el) => { console.log(el); };
// Spread operator
// Array.from()
// for...of statement
for (var div of elements) callback(div);
Copy link

bendc commented Jan 13, 2015

Unsurprisingly, for...of is approximately twice as fast as the two other approaches.

Copy link

kellengreen commented Dec 1, 2015

Unfortunately Chrome (46 at the time of this writing) doesn't support for...of iteration on NodeLists.

Copy link

martinmcwhorter commented Jun 17, 2016

In chrome 51 you can just do elements.forEach(callback)

Copy link

varkadov commented Aug 1, 2016

Array.from(elements, callback)

Copy link

treeskar commented Sep 1, 2016

You also can add custom iterator


Copy link

mzalazar commented Jan 3, 2019

[], callback)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment