DOM Methods

NodeList.entries()

Returns an iterator of index/node pairs rather than name/value pairs. Use a for...of loop or spread. Destructure each pair as [index, node] in a for...of loop to display or inspect the list with numbering. These indices are positions, not stable node identities; changes to live lists can affect the iteration. Example result: [[0,"A"],[1,"B"]].

Formal Syntax & Structure

html syntaxhtml
list.entries()

Return Value

iterator

Iterator yielding [index, Node] pairs.

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.querySelectorAll("span");  result.textContent = JSON.stringify([...list.entries()].map(([i,n])=>[i,n.textContent]));  })().catch(error => { result.textContent = "Error: " + error.name; });</script>
HTML Reference