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
Fundamentals
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.
Concept
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).
Foundation
📝 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);
}
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported on NodeList.length (legacy DOM)
Legacy OK
NodeList.lengthExcellent
Bottom line: Read list.length before looping—on live lists, the count can change when the DOM changes.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about NodeList.length
How many nodes are in the list.
5
Core concepts
🔗01
Returns
integer
API
⚙️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.