DOM: Node vs Element, NodeList vs HTMLCollection
What is NodeList, HTMLCollection?
NodeList and HTMLCollection are both Array-Like Objects that are returned by many DOM methods.
They are collection of Nodes.
Iterable Object
NodeList and HTMLCollection are Iterable Objects. You can use for-of Loop on them.
Array-Like Object
NodeList and HTMLCollection are Array-Like Objects. [see Apply Array Method to Array-Like Object]
is Live Object?
- NodeList and HTMLCollection may or may not, be Live Object. e.g.
document.querySelectorAll
return a non-live NodeList. - There's no builtin way to find out whether NodeList or HTMLCollection is live object.
What Does getElementsByClassName Return?
How to Determine NodeList or HTMLCollection
You can find out the type by:
let xx = document.getElementsByClassName("xx"); Reflect.apply(Object.prototype.toString, xx, []);
It'll return a string like
"[object HTMLCollection]"
or
"[object NodeList]"
.
[see Determine Type of Object]