DOM Methods

NodeList.keys()

Returns an iterator of zero-based list indices. This operation yields numbers, not the nodes themselves or their IDs. Use values() for nodes and entries() when both position and node are needed. Example result: [0,1].

Formal Syntax & Structure

html syntaxhtml
list.keys()

Return Value

iterator

Iterator yielding zero-based numeric indices.

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.keys()]);  })().catch(error => { result.textContent = "Error: " + error.name; });</script>
HTML Reference