/
NodeList and HTMLCollection

NodeList and HTMLCollection

If it looks like a duck and walks like a duck then is it a duck?

In an ideal world yes NodeList and HTMLCollection objects would be replaced by arrays.

Why do these objects exist in the world of Javascript?

I think it’s a legacy issue. It would be simpler if these objects didn’t exist - most of the time you can rely on the fact that they:

  • Support the .length property

  • And the [] operating to access elements

That they can be treated as arrays. And yet they are not which largely comes down to legacy backwards compatibility issue.

It’s an example of how Javascript has less symmetry than Lua.

See this video that shows one API which makes use of the NodeList object.

See:

HTMLCollection - Web APIs | MDN

NodeList - Web APIs | MDN

Related content

DOM Elements versus HTML markup
DOM Elements versus HTML markup
More like this
DOM Element
DOM Element
Read with this
Javascript Containers
Javascript Containers
More like this
How can I reset the browser through Javascript?
How can I reset the browser through Javascript?
Read with this
What is a DOM element under the hood?
What is a DOM element under the hood?
More like this
How do I change the content of a DOM element?
How do I change the content of a DOM element?
Read with this