DOM Properties
HTMLCollection.length
Counts currently matched elements. Because the collection is live, its count may change during iteration. A loop that removes matching elements may change the next index as it runs. When stable membership is required, copy the collection first; when a live count is desired, read length again after modifying the document. Example result: 2.
Formal Syntax & Structure
html syntaxhtml
list.lengthReturn Value
number
Current number of elements in the live collection.
Working Example
Example Snippethtml
<div id="items"> <span id="first">A</span> <span>B</span></div><p> Result: <output id="result"></output></p><script> const result = document.getElementById("result"); (async () => { const items = document.getElementById("items"); const list = items.children; result.textContent = list.length; })().catch(error => { result.textContent = "Error: " + error.name; });</script>