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:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection

https://developer.mozilla.org/en-US/docs/Web/API/NodeList

Related pages