J

JavaScript Handbook

Clean • Professional

DOM Collections in JavaScript

3 minute

DOM Collections

In JavaScript, when you access multiple elements from the DOM (like all <p> tags or all elements with a specific class), they are often returned as a DOM Collection — a special type of array-like object that represents multiple DOM nodes.

DOM collections allow you to loop through, access, and manipulate multiple elements in the document.

What is a DOM Collection?

A DOM Collection is an array-like list of nodes (elements) returned by DOM methods such as:

  • document.getElementsByTagName()
  • document.getElementsByClassName()
  • document.images
  • document.forms
  • document.links
  • document.scripts
  • element.children

These collections are not true arrays, but array-like objects — meaning:

  • You can access items using index numbers (e.g., collection[0])
  • You can check the length (collection.length)
  • But you can’t directly use array methods like .map(), .filter(), etc.

Types of DOM Collections

There are two major types of DOM collections:

HTMLCollection

A live collection of elements — it updates automatically when the DOM changes.

An HTMLCollection is returned by methods like:

  • document.getElementsByTagName()
  • document.getElementsByClassName()
  • document.forms
  • element.children

Example:

<!DOCTYPE html>
<html>
<body>

<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>

<script>
const paras = document.getElementsByTagName("p");

console.log(paras);           // HTMLCollection(3)
console.log(paras[0].innerHTML);  // Paragraph 1
console.log(paras.length);        // 3
</script>

</body>
</html>

HTMLCollection Example (Live Behavior)

<ul id="list">
  <li>Apple</li>
  <li>Banana</li>
</ul>

<script>
const items = document.getElementsByTagName("li");
console.log(items.length); // 2

// Add a new list item
const newItem = document.createElement("li");
newItem.textContent = "Cherry";
document.getElementById("list").appendChild(newItem);

console.log(items.length); // 3  Automatically updated
</script>

NodeList

A collection of nodes — it can be live or static, depending on how it’s created.

A NodeList is returned by:

  • document.querySelectorAll()
  • childNodes
  • document.getElementsByName()

Example:

<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>

<script>
const nodeList = document.querySelectorAll("p");
console.log(nodeList); // NodeList(3)
console.log(nodeList[0].textContent); // Paragraph 1
</script>

NodeList Example (Static Behavior)

<ul id="list">
  <li>Dog</li>
  <li>Cat</li>
</ul>

<script>
const items = document.querySelectorAll("li");
console.log(items.length); // 2

// Add a new <li> dynamically
const newItem = document.createElement("li");
newItem.textContent = "Bird";
document.getElementById("list").appendChild(newItem);

console.log(items.length); // 2  Not updated (static NodeList)
</script>

Difference Between HTMLCollection & NodeList

FeatureHTMLCollectionNodeList
Returned bygetElementsByTagName, getElementsByClassNamequerySelectorAll, childNodes
ContainsOnly element nodesCan include text, comment, or element nodes
Live / StaticLiveUsually Static
forEach() SupportNoYes
Updates with DOM changesYesNo (unless live NodeList)

Looping Through DOM Collections

Using a for loop

const paras = document.getElementsByTagName("p");

for (let i = 0; i < paras.length; i++) {
  console.log(paras[i].innerText);
}

Using for...of

for (let element of document.querySelectorAll("p")) {
  console.log(element.textContent);
}

Using forEach() (NodeList only)

document.querySelectorAll("p").forEach((p) => {
  p.style.color = "blue";
});

Converting a Collection to an Array

Because collections are array-like, not real arrays —

you can convert them to use array methods like map, filter, etc.

Using Array.from()

const divs = document.getElementsByTagName("div");
const divArray = Array.from(divs);
divArray.forEach(div => div.style.background = "yellow");

Using Spread Operator

const items = [...document.querySelectorAll("li")];
items.map(item => console.log(item.textContent));

Common DOM Collections

PropertyDescription
document.imagesCollection of all <img> elements
document.formsCollection of all <form> elements
document.linksCollection of all <a> elements with href
document.scriptsCollection of all <script> elements
element.childrenHTMLCollection of child elements (no text nodes)
element.childNodesNodeList of all child nodes (includes text/comments)

Example:

console.log(document.forms.length);   // Number of forms
console.log(document.images[0].src);  // First image source

 

Article 0 of 0