Why aren’t nodelists arrays?
I’d love to hear your thoughts on this.
He let me reprint his email and my response here. So here’s the deal…
(I actually didn’t know this until you asked the question. Crazy, right?)
For example, on MDN they provide an example of using Python and getElementsByTagName:
# Python DOM example import xml.dom.minidom as m doc = m.parse("C:\\Projects\\Py\\chap1.xml"); doc.nodeName # DOM property of document object; p_list = doc.getElementsByTagName("para");
NodeLists differ from Arrays in another meaningful way, too.
They are often (but not always) live lists, meaning that if elements are removed or added to the DOM, the list updates automatically.
querySelectorAll() return a static list (one that doesn’t update), but properties like
.childNodes are live lists that will change as you manipulate the DOM (which can be a good or bad thing, depending on how you’re using it).
This is all made more confusing because arrays can contain nodes. And, there’s another, older type of list called an HTMLCollection that predates NodeLists, but is functionally similar (another article for another day).
They each have their own methods and properties, and you can convert a NodeList into an Array if you need to (but not the other way around).
🔥 Hot off the press! I just launched a new pocket guide. Learn about ES6 arrow functions,
const, function hoisting, and more.