CKTIS / JavaScript
HTMLCollection.length — number of elements in a collection
HTMLCollection.length returns the current number of elements in a DOM collection. It is read-only, and its value updates automatically when the membership of the live collection changes.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
collection.lengthHTMLCollection.length is a read-only property returning a nonnegative integer: the number of elements in the collection. Read it as collection.length, without call parentheses. An empty collection has a length of 0. Indices start at 0, so the last index of a nonempty collection is collection.length - 1.
An HTMLCollection is live: DOM changes affecting its membership automatically update the collection and its length. In the example, container.children includes only direct children that are elements. Adding a text node does not increase the count, but adding an element does. The same items reference also reflects the subsequent removal of the element.
Do not treat an HTMLCollection as an array: assigning to length is not a way to truncate it. Change the relevant DOM elements instead. Removing elements during iteration can shift indices and cause entries to be skipped; if you plan such changes, iterate over a copy created with Array.from(collection).
This is a browser DOM API, not a feature of a particular ECMAScript edition. MDN marks this property as widely available across browsers since July 2015. The presence of Node 24 in a repository does not establish DOM availability or browser support for this API.
Examples
(() => {
const container = document.createElement("div");
const items = container.children;
console.log(items.length);
container.appendChild(document.createTextNode("text"));
console.log(items.length);
const child = document.createElement("span");
container.appendChild(child);
console.log(items.length);
container.removeChild(child);
console.log(items.length);
})();Result
0 0 1 0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.