JavaScript NodeList length Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

The length property of a NodeList returns the number of items in the list. Learn how to read list.length, use it in classic for loops, check for empty lists, and understand live vs static NodeLists—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

Integer

03

Writable?

Read-only

04

Empty?

length === 0

05

Loops

i < length

06

Status

Baseline widely

Introduction

Every NodeList knows how many nodes it holds. list.length is that count—an integer from 0 upward. It is one of the most-used properties in DOM scripting because loops and empty checks depend on it.

MDN’s classic pattern: get all paragraphs, then loop for (let i = 0; i < items.length; i++) to read each node at index i.

💡
Beginner tip

length tells you how many nodes exist. Use item() or list[i] to fetch the node at a given index.

Understanding the length Property

A read-only instance property on NodeList that returns how many items the list currently contains.

  • Returns — a non-negative integer.
  • Read-only — assigning to length has no effect.
  • Indexes run from 0 to length - 1.
  • Live lists (e.g. childNodes) update length when the DOM changes.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Read the property on a NodeList:

JavaScript
list.length

Return value

An integer representing the number of items in the NodeList.

Typical pattern (MDN idea)

JavaScript
const items = document.getElementsByTagName("p");

for (let i = 0; i < items.length; i++) {
  console.log(items[i].innerHTML);
}

⚡ Quick Reference

GoalCode / note
Get countlist.length
Classic loopfor (let i = 0; i < list.length; i++)
Empty checklist.length === 0
Last indexlist.length - 1
Writable?No (read-only)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NodeList.length.

Type
integer

Node count

Writable?
no

Read-only

Empty?
0

Zero items

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NodeList.length. Labs use querySelectorAll, getElementsByTagName, and childNodes.

📚 Getting Started

Read and use length the MDN way.

Example 1 — Read length From querySelectorAll

Count matched list items in the document.

JavaScript
const items = document.querySelectorAll("li");
console.log(items.length);
// 3
Try It Yourself

How It Works

Three matching <li> elements produce length === 3.

Example 2 — Classic for Loop (MDN Idea)

Use length as the loop bound and list[i] to read each node.

JavaScript
const items = document.getElementsByTagName("p");
let combined = "";

for (let i = 0; i < items.length; i++) {
  combined += items[i].textContent;
}

console.log(combined);
Try It Yourself

How It Works

MDN’s pattern: i runs from 0 up to (but not including) length.

📈 Checks & Live Lists

Empty tests, childNodes, and live length updates.

Example 3 — Test Whether a List Is Empty

Use length === 0 before looping or showing UI.

JavaScript
const items = document.querySelectorAll(".missing-class");

if (items.length === 0) {
  console.log("No matches");
} else {
  console.log("Found", items.length);
}
Try It Yourself

How It Works

When nothing matches the selector, length is 0.

Example 4 — childNodes.length Includes Text Nodes

Count all direct children, not just elements.

JavaScript
const box = document.getElementById("box");
console.log(box.childNodes.length);
// often > box.children.length when whitespace exists
Try It Yourself

How It Works

Whitespace text nodes increase childNodes.length beyond element-only counts.

Example 5 — Live List: length Changes With the DOM

On a live NodeList, adding a node updates length.

JavaScript
const list = document.getElementById("live").childNodes;
console.log("before", list.length);

document.getElementById("live").appendChild(document.createElement("span"));
console.log("after", list.length);
Try It Yourself

How It Works

childNodes is live—the same NodeList object reflects DOM changes in length.

🚀 Common Use Cases

  • Bound a classic for loop: i < list.length.
  • Check whether any nodes matched: list.length > 0.
  • Get the last index: list.length - 1.
  • Compare counts before and after DOM updates on live lists.
  • Decide whether to run logic when a selector finds nothing.

🔧 How It Works

1

Get a NodeList

From querySelectorAll, childNodes, getElementsByTagName, etc.

DOM
2

Read length

list.length returns the current integer count of nodes in the list.

Property
3

Use the count

Loop with i < length, test for zero, or compute last index.

Apply
4

Stay in sync

On live lists, re-read length after DOM changes so loops stay correct.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • length is read-only; you cannot set it directly.
  • Valid indexes: 0 through length - 1.
  • querySelectorAll returns a static NodeList; childNodes is live.
  • Related learning: item(), forEach(), childNodes, entries().

Universal Browser Support

NodeList.length is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

NodeList.length

Returns the number of nodes in the list. Read-only integer used in loops and empty checks.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NodeList.length (legacy DOM)
Legacy OK
NodeList.length Excellent

Bottom line: Read list.length before looping—on live lists, the count can change when the DOM changes.

Conclusion

NodeList.length is the node count for any NodeList. Use it in loops, empty checks, and last-index math. Remember it is read-only and may change on live lists when the DOM updates.

Continue with NodeIterator.filter, item(), forEach(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use list.length === 0 for empty checks
  • Loop with i < list.length in classic for loops
  • Re-read length after DOM changes on live lists
  • Use forEach or for...of when you do not need indexes
  • Remember valid indexes end at length - 1

❌ Don’t

  • Try to assign a new value to list.length
  • Assume childNodes.length equals element count only
  • Use i <= list.length (off-by-one past last index)
  • Cache length forever on a live list you are mutating
  • Forget that an empty match still returns a NodeList with length 0

Key Takeaways

Knowledge Unlocked

Five things to remember about NodeList.length

How many nodes are in the list.

5
Core concepts
⚙️02

Read-only

no assign

Rule
🔒03

Empty

length 0

Check
📄04

Loop

i < length

Pattern
🎯05

Baseline

since Jul 2015

Status

❓ 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++) { ... list[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 or getElementsByTagName, length changes when matching nodes are added or removed from the DOM.
Did you know?

MDN highlights length as the usual loop bound in DOM code. That classic for (let i = 0; i < items.length; i++) pattern still appears everywhere—even when forEach or for...of would be shorter.

Explore NodeIterator.filter

Next up: read the NodeFilter that screens iterator walks.

filter property →

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.

5 people found this page helpful