Back to JavaScript Notes
Topic #247

DOM Collections

DOM collections like HTMLCollection are live lists that update with the document. Their live nature surprises newcomers.


The HTMLCollection Object

The getElementsByTagName() method returns an HTMLCollection object.

An HTMLCollection object is an array-like list (collection) of HTML elements.

The following code selects all <p> elements in a document:

Example

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

Note: The index starts at 0.


HTML HTMLCollection Length

The length property defines the number of elements in an HTMLCollection:

Example

myCollection.length

The length property is useful when you want to loop through the elements in a collection:

Example

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

for (let i = 0; i < myCollection.length; i++) {

  myCollection[i].style.color = "red";

}

Note: An HTMLCollection is NOT an array! An HTMLCollection may look like an array, but it is not. You can loop through the list and refer to the elements with a number (just like an array). However, you cannot use array methods like valueOf(), pop(), push(), or join() on an HTMLCollection.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

DOM Collections – FAQs

Quick answers about learning DOM Collections in JavaScript.

This free note from Coding Now Tech Institute explains DOM Collections in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including DOM Collections, is 100% free with no signup required.
With focused practice, most students grasp DOM Collections in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now