🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #248

DOM Node Lists


The HTML DOM NodeList Object

A NodeList object is a list (collection) of nodes extracted from a document.

A NodeList object is almost the same as an HTMLCollection object.

Some (older) browsers return a NodeList object instead of an HTMLCollection for methods like getElementsByClassName().

All browsers return a NodeList object for the property childNodes.

Most browsers return a NodeList object for the method querySelectorAll().

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

Example

const myNodeList = document.querySelectorAll("p");

Note: The index starts at 0.


HTML DOM Node List Length

The length property defines the number of nodes in a node list:

Example

myNodelist.length

The length property is useful when you want to loop through the nodes in a node list:

Example

const myNodelist = document.querySelectorAll("p");

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

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

}

The Difference Between an HTMLCollection and a NodeList

A NodeList and an HTMLcollection is very much the same thing.

Both are array-like collections (lists) of nodes (elements) extracted from a document. The nodes can be accessed by index numbers. The index starts at 0.

Both have a length property that returns the number of elements in the list (collection).

An HTMLCollection is a collection of document elements.

A NodeList is a collection of document nodes (element nodes, attribute nodes, and text nodes).

HTMLCollection items can be accessed by their name, id, or index number.

NodeList items can only be accessed by their index number.

An HTMLCollection is always a live collection. Example: If you add a

  • element to a list in the DOM, the list in the HTMLCollection will also change.

    A NodeList is most often a static collection. Example: If you add a

  • element to a list in the DOM, the list in NodeList will not change.

    The getElementsByClassName() and getElementsByTagName() methods return a live HTMLCollection.

    The querySelectorAll() method returns a static NodeList.

    The childNodes property returns a live NodeList.


    Note: Not an Array! A NodeList may look like an array, but it is not. You can loop through a NodeList and refer to its nodes by index. But, you cannot use Array methods like push(), pop(), or join() on a NodeList.

  • Want to go beyond the notes?

    Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

    Enroll Now — Free Demo Available

    DOM Node Lists – FAQs

    Quick answers about learning DOM Node Lists in JavaScript.

    This free note from CodingNow 2.0 explains DOM Node Lists in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
    Yes. Every JavaScript topic on CodingNow 2.0, including DOM Node Lists, is 100% free with no signup required.
    With focused practice, most students grasp DOM Node Lists in 1–3 days from these notes; pairing it with CodingNow 2.0'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 CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
    WhatsApp
    Call NowEnroll Now