Skip to main content

Knowing the Difference between HTMLCollections and Nodelists

Updated 6 min readMarkdown
Knowing the Difference between HTMLCollections and Nodelists

I was being friendly with the DOM and i have used some document methods and as i used the document.getElementsByTagname() i received an collection of elements with the same tagname i have used in my web page and i realised that the collection is not the same as the array means all the properties of array cannot be used on the collection so i got curious about it and now i am writing to explain the same through this blog.

let's get started with the introduction to the HTMLcollection and Nodelists.


What is a HTMLCollection ?

The HTMLCollection is the interface to the collection of elements in the document order. It seems to be an array because it contains some properties similar to array like .length and indexing too but it is not a true JavaScript Array.

You can get confused by its name as if it returns the HTML related to the selected object but it is not true it strictly return the reference to the actual element in the DOM.

How They can be Obtained?

The HTMLCollections are not created by the user manually but they are the output returned by using some commands in the JavaScript while getting the elements .

  1. getElementsByTagname() :-

When we have to access the elements using the tag names means when we need a collection of the elements with the same tag used in the webPage we use this command and this command returns a HTMLCollection .

for example :-

I have created a Web page with three list item i.e. li items and you can see in the below image what it returns.

2. getElementsByClassName()

These command we usually run on the document object and it return the collection of the elements used in the webpage with the same className.

<div class="box">box1</div>
<div class="box">box2</div>
<div class="box">box3</div>
const arr = document.getElementsByClassName("box"); 

output is :

The HTML collection with the class name box.

HTMLCollection(3) [div.box, div.box, div.box]
0: div.box
1: div.box
2: div.box
length: 3
[[Prototype]]: HTMLCollection

similary this can be obtained with the getElements by command and it always return a HTMLCollection.

Things to know About HTMLCollection :

  • The elements in the HTMLCollection are actually the reference the the original element present in the DOM.

  • The HTMLCollection updates live if any change is made in the DOM.

  • You cannot actually change the elements inside the HTMLCollection.

  • Most importantly the HTMLCollection store the elements only , not the nodes , while the Nodelist stores all the nodes even the textnodes.

  • you cannot use methods like .map() , .push() and .pop() , etc.

What does live means here?

If the DOM changes a single element in the webpage the HTMLCollections are directly affected . Let us understand it with an example :

<ul id="ul">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

As you can see we are having a unordered list with three list items. We will try to create a variable and store the HTMLCollection in that variable.

const arr = document.getElementsByTagName("li");
const li = document.createElement("li");
const ul = document.getElementById("ul");
li.textContent = "Item 4";

we have created an array that is having storing the HTMLCollection and created an ul and li items to create the changes in the web page .

Now everything is ready just move to browser console :

As you can observe we haven't made any changes in the arr , at first it was containing three elements as i have added an extra elements the arr updated automatically , that is what we refer as the live .

If you think it is normal , no it is not because we haven't updated changes in the arr it automatically detected the extra element in the dom and it got added in the arr.


What are the Nodelist ?

As the name suggest it is the container of the nodes present in the DOM it can contain any type of node . It can be said that it is a unique object type of a segment of the DOM.

The nodes can be of different types :

  • comment node

  • text node

  • element node , etc.

How They can be Obtained?

they can be obtained using the document model commands like :

  • .querySelectorAll()

  • .childNodes()

There is a main difference between the two that we will discuss here.

1. .querySelectorAll()

This is the most used command in the js and it return the nodelist. This is a static type of nodelist that don't update automatically as the DOM changes.

lets have an example :

<ul id="ul">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>
const arr = document.querySelectorAll("li");
console.log(arr);

output is :-

2. .childNodes()

This command works the same but it return the live Nodelist means it is like mirror of the dom or you can say similar to the HTMLCollection. Any changes in the Dom will be reflected in the Nodelist.

lets take the same example of the queryselector just we wil use the .Childnode instead of the .queryselector.

const arr = document.getElementById("ul").childNodes;
console.log(arr);

the output is :-

NodeList(7) [text, li, text, li, text, li, text];

Difference between HTMLcollection and Nodelist.

HTMLCollection Nodelist
It is always live. These are mostly Static but , childNode is the exception having a live nodelist.
array methods cannot be used. array methods can be used.
only consists the elements it consists of the nodes : text , comment and element ,etc .
methods can be used to obtain, like getElementsByTagName(), getElementsByClassName(), .children. methods can be used to obtain are querySelectorAll(), .childNodes.

Conclusion:

HTMLCollections and NodeLists both serve as array-like containers for DOM elements, they differ fundamentally in their scope and "live" behavior. An HTMLCollection is a live reflection of only element tags, making it useful for real-time DOM tracking but potentially tricky during loops. In contrast, a NodeList (via querySelectorAll) acts as a static snapshot of any node type—including text and comments—offering more predictable results for modern development.

They are having their own usecases depending on the usecases the HTMLCollection and the Nodelists can be used , none of them is useless and none is useful both are good.

No comments yet. Be the first to comment.