Although NodeList is not an Array, it is possible to iterate over it with forEach(). It can also be converted to a real Array using Array.from().
However, some older browsers have not implemented NodeList.forEach() nor Array.from(). This can be circumvented by using Array.prototype.forEach() — see this document's Example.
It’s not that dealing with this one way or another is that much of a pain, it’s just frustrating that it often trips people up when they’re expecting array behavior and don’t know what to do when it’s almost array behavior.
Things like Set and Map do well to hint that they are not arrays by using the size property instead of length. Should NodeList perhaps implement this prop name hint, or is it just a matter of browser support?
Speaking of trip ups:
NodeList
.Looks and smells like an Array but don’t try calling
forEach
!Huh. MDN docs @ developer.mozilla.org/en-US/docs/W... says this:
Although NodeList is not an Array, it is possible to iterate over it with forEach(). It can also be converted to a real Array using Array.from().
However, some older browsers have not implemented NodeList.forEach() nor Array.from(). This can be circumvented by using Array.prototype.forEach() — see this document's Example.
I'm not too familiar with NodeList though.
It’s not that dealing with this one way or another is that much of a pain, it’s just frustrating that it often trips people up when they’re expecting array behavior and don’t know what to do when it’s almost array behavior.
Things like Set and Map do well to hint that they are not arrays by using the
size
property instead oflength
. Should NodeList perhaps implement this prop name hint, or is it just a matter of browser support?NodeList should inherit Array though. Set and Map are entirely different data structures.