JavaScript NodeList length Property

Beginner
5 min read
Updated: Nov 2026
5 examples
Property

Definition

NodeList.length returns the number of nodes in the list as an integer (MDN). Empty lists have length 0. The property is read-only.

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use nodeList.length. Start with this line.

JavaScript
nodeList.length
CallWhat it does
nodeList.lengthPrimary signature.

Built into the browser. Read nodeList.length on a NodeList — no import required.

Library declarations
JavaScript
nodeList.length

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*The current value of nodeList.length.

JavaScript Examples

Example 1 — Basic Count

Read how many elements match a selector.

JavaScript
// Imagine: <ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>
const items = document.querySelectorAll("li");
console.log(items.length); // 3
Try It Yourself

How It Works

  1. Select elements. querySelectorAll returns a NodeList.
  2. Read length. One integer for the whole list.
  3. Three matches. Three <li> → 3.
  4. Tip. Same idea as array.length, but for DOM nodes.

Example 2 — Empty Check

MDN tip: test length before you assume nodes exist.

JavaScript
const missing = document.querySelectorAll(".no-such-class");
const found = document.createElement("div");
found.appendChild(document.createElement("p"));

console.log(missing.length === 0);           // true
console.log(found.childNodes.length > 0);    // true
Try It Yourself

How It Works

  1. No matches. Selector finds nothing → length is 0.
  2. Has children. One child → length > 0.
  3. Guard first. Avoid calling item(0) on an empty list without a check.
  4. Clear intent. === 0 reads better than relying on falsy alone.

Example 3 — for + length

Classic index loop using length as the stop condition.

JavaScript
const node = document.createElement("div");
node.appendChild(document.createElement("p"));
node.appendChild(document.createElement("span"));

const list = node.childNodes;
const names = [];
for (let i = 0; i < list.length; i++) {
  names.push(list.item(i).nodeName);
}
console.log(names.join(", ")); // P, SPAN
Try It Yourself

How It Works

  1. Bound the loop. i < list.length visits every index.
  2. Fetch by index. item(i) (or list[i]).
  3. Collect names. Side effect while counting stays correct.
  4. Modern tip. Prefer forEach() when you do not need the index control.

Example 4 — childNodes Text Nodes

childNodes.length counts text nodes too — not only elements.

JavaScript
const node = document.createElement("div");
node.appendChild(document.createElement("p"));
node.appendChild(document.createTextNode("hey"));
node.appendChild(document.createElement("span"));

console.log(node.childNodes.length); // 3
console.log(node.children.length);   // 2  (elements only)
Try It Yourself

How It Works

  1. Three child nodes. Element, text, element.
  2. childNodes.length → 3 (includes #text).
  3. children.length → 2 (elements only).
  4. Watch whitespace. Pretty-printed HTML often adds extra text nodes.

Example 5 — Live vs Static

Live childNodes updates; querySelectorAll keeps its snapshot.

JavaScript
const wrap = document.createElement("div");
wrap.innerHTML = "<span class='x'>A</span>";

const live = wrap.childNodes;
const snap = wrap.querySelectorAll(".x");

console.log(live.length, snap.length); // 1 1

wrap.appendChild(document.createElement("span")).className = "x";

console.log(live.length, snap.length); // 2 1
Try It Yourself

How It Works

  1. Start with one child. Both counts are 1.
  2. Append another. DOM gains a second <span>.
  3. Live list. childNodes.length becomes 2.
  4. Static list. querySelectorAll stays at 1 until you query again.

Browser Support

NodeList.length is marked Baseline Widely available on MDN (since July 2015). DOM API — not a Node.js built-in.

✓ Baseline · Widely available

length

Read how many nodes are in a NodeList across modern browsers.

99% Core tag support
Chrome 1+
Full support
Edge 12+
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Safe for production. Read length for empty checks and loop bounds; remember live vs static lists.

Frequently Asked Questions

An integer—the number of items (nodes) currently in the NodeList. If the list is empty, length is 0.
No. MDN marks NodeList.length as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. length is read-only. You cannot assign to it. Add or remove nodes in the DOM (for live lists) or get a new NodeList to change the count.
Classic pattern: for (let i = 0; i < list.length; i++) { ... }. You can also use forEach, for...of, or iterator methods.
Yes. childNodes.length counts every direct child node—elements, text, and comments—not just HTML elements.
Yes. On live lists like childNodes, length changes when matching nodes are added or removed. querySelectorAll returns a static snapshot.

Next: NodeIterator filter

See which nodes a NodeIterator accepts.

filter →

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.

8 people found this page helpful