JavaScript NodeList length 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.
| Call | What it does |
|---|
nodeList.length | Primary signature. |
Built into the browser. Read nodeList.length on a NodeList — no import required.
Library declarations
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
| Return Value | Description |
|---|
| * | The current value of nodeList.length. |
JavaScript Examples
Example 1 — Basic Count
Read how many elements match a selector.
// Imagine: <ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>
const items = document.querySelectorAll("li");
console.log(items.length); // 3
How It Works
- Select elements.
querySelectorAll returns a NodeList. - Read
length. One integer for the whole list. - Three matches. Three
<li> → 3. - Tip. Same idea as
array.length, but for DOM nodes.
Example 2 — Empty Check
MDN tip: test length before you assume nodes exist.
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
How It Works
- No matches. Selector finds nothing →
length is 0. - Has children. One child →
length > 0. - Guard first. Avoid calling
item(0) on an empty list without a check. - Clear intent.
=== 0 reads better than relying on falsy alone.
Example 3 — for + length
Classic index loop using length as the stop condition.
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
How It Works
- Bound the loop.
i < list.length visits every index. - Fetch by index.
item(i) (or list[i]). - Collect names. Side effect while counting stays correct.
- 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.
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)
How It Works
- Three child nodes. Element, text, element.
childNodes.length → 3 (includes #text).children.length → 2 (elements only).- Watch whitespace. Pretty-printed HTML often adds extra text nodes.
Example 5 — Live vs Static
Live childNodes updates; querySelectorAll keeps its snapshot.
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
How It Works
- Start with one child. Both counts are
1. - Append another. DOM gains a second
<span>. - Live list.
childNodes.length becomes 2. - Static list.
querySelectorAll stays at 1 until you query again.
Compatibility
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.
Chrome 1+
Full support
Edge 12+
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full supportHTML 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
Developer, cloud engineer, and technical writer
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