DOM: NodeList vs HTMLCollection

By Xah Lee. Date: . Last updated: .

Difference Between NodeList, HTMLCollection

NodeList is a collection of nodes. Node includes any HTM/XML element, and also text content of a element, such as inner text of <p>…</p>.

HTMLCollection is a collection any HTM/XML elements. It has method namedItem, which is not in NodeList. (Opera browser adds the method to its NodeList object.)

Array-Like Object

• NodeList and HTMLCollection are array-like objects. Meaning, they have length property, and have properties named 0, 1, 2, …, n, so that you can access its elements like a array.

To use array methods on array-like object, call the method like this:, function)

[see JS: Array-Like Object]

[see JS: Array-Like Object to Array]

[see JS: Array.prototype]

Live Object or No?

• NodeList and HTMLCollection are NOT ALWAYS live objects. For example: document.querySelectorAll return a non-live object. [see DOM: Get Elements by ID, Tag, Name, Class, CSS Selector]

• There's no builtin way to find out whether NodeList or HTMLCollection is live object.

• When working with live objects, you must be careful, not to insert or remove element within a loop. [see DOM: What Does Live Object Mean?]

You should NOT use for (var x in object) {…}, because that loops thru all enumerable properties and goes into prototype chain. [see JS: Access Property]

The most supported and safe and fast way to loop thru array-like object is a normal loop, like this:

for (let i = 0; i < myNodeList.length; ++i) {… myNodeList[i] …}

How to Convert Array-Like Object to Array?

See: JS: Array-Like Object to Array

What Does getElementsByClassName Return?

As of , {Firefox, Internet Explorer} return HTMLCollection. {Chrome, Safari, Opera} return NodeList. Older versions of Firefox returns NodeList.

[see DOM: Get Elements by ID, Tag, Name, Class, CSS Selector]

Even Browsers and W3C Are Confused!

Here's what Mozilla says:

Note: While the W3C DOM 3 Core specification says elements is a NodeList that was simply because of a an attempt to have the “core” specification not depend on the “html” specification at that time. The DOM 4 draft says that elements is an HTMLCollection.

Gecko/Firefox currently returns a NodeList (Bug 162927) but starting with Gecko/Firefox 19, this method will return HTMLCollection (Bug 799464). Internet Explorer returns a HTMLCollection. WebKit returns a NodeList. Opera also returns a NodeList, but with a namedItem method implemented, which makes it similar to a HTMLCollection.

, from

Here's WHATWG says as of 2016-07-02:

Elements is the better solution for representing a collection of elements. HTMLCollection is an historical artifact we cannot rid the web of.

However, as of today , Google Chrome and Firefox returns HTMLCollection.

You can find out the type by: .getElementsByClassName("xyz"))

Web Scripting Overview

  1. Browser Window Object, Document Object Model
  2. Intro to Event-Based Programing
  3. JavaScript Load Order
  4. What is Live Object
  5. NodeList vs HTMLCollection
  6. DOM: Whitespace Nodes
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog