JavaScript NodeList

Beginner
8 min read
Updated: Nov 2026
3 examples
Node collections

Definition and Usage

A NodeList is an array-like list of nodes. The most common source is document.querySelectorAll("li"), which returns a static NodeList of matching elements.

Read length, index with list[0] or item(0), and loop with forEach. Methods such as entries, keys, and values support for...of. Some NodeLists (like childNodes) are live and update when the tree changes.

This page teaches what NodeList is. Method and property guides live under this section.

Not a real Array.

NodeList has length and forEach, but not map. Use Array.from(list) when you need full array methods.

JavaScript Syntax

Working with a NodeList:

JavaScript
const items = document.querySelectorAll("li");
console.log(items.length);
items.forEach(function (el, i) {
  console.log(i, el.textContent);
});

Core Ideas

Ideas every NodeList tutorial builds on:

IdeaExampleRemember
Querydocument.querySelectorAll("li")Static NodeList of matches
lengthlist.lengthHow many nodes
Indexlist[0]First node or undefined
forEachlist.forEach(fn)Loop each node
entrieslist.entries()Index/node pairs
Live vs staticchildNodes vs qSALive lists can change

JavaScript Examples

Three short programs that show how NodeList works.

Example 1 — Count matches

length is the number of nodes in the list.

JavaScript
const items = document.querySelectorAll(".card");
console.log(items.length);

How It Works

  1. querySelectorAll builds a NodeList.
  2. length is how many elements matched.
  3. An empty match still returns a NodeList with length 0.

Example 2 — Loop with forEach

forEach calls your function once per node.

JavaScript
const items = document.querySelectorAll("li");
items.forEach(function (el) {
  console.log(el.textContent);
});

How It Works

  1. forEach walks each node in order.
  2. The callback receives the element.
  3. No need to convert to an array for simple loops.

Example 3 — Index pairs with entries

entries() yields [index, node] pairs for for...of.

JavaScript
for (const [i, el] of items.entries()) {
  console.log(i + ":" + el.tagName);
}

How It Works

  1. entries() returns an iterator.
  2. Destructure [i, el] in the loop.
  3. Indexes start at 0 like arrays.

Next: NodeList Guides

Continue with entries, forEach, and the other list methods.

Look upWhy
entries()Index/node pairs
forEach()Loop each node
item()Node at an index
keys() / values()Iterators for indexes or nodes

Full lists: entries().

Frequently Asked Questions

A NodeList is an array-like collection of DOM nodes, commonly returned by querySelectorAll. It has length and can be looped with forEach.
No. It is array-like. Use Array.from(nodeList) or [...nodeList] when you need map, filter, or other array methods.
A live list updates when the document changes. childNodes is live. querySelectorAll returns a static list that does not auto-update.
Use list[0] or list.item(0). If the list is empty, both give undefined/null respectively—check length first.
Open entries() and forEach(), then Document querySelectorAll examples.

Open entries()

Then explore forEach, item, keys, and values.

entries() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful