DOM Methods

NodeList.forEach()

Visits each node in list order with node, index and list arguments. It is not Array.map, and async callbacks are not awaited. Example result: AB.

Formal Syntax & Structure

html syntaxhtml
list.forEach(callback, thisArg)

Parameters & Attributes

NameTypeDescription
callbackfunctionReceives node, index and list.
thisArganyOptional callback this value.

Return Value

undefined

Node list operation result.

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");  const parts=[]; list.forEach(node=>parts.push(node.textContent)); result.textContent=parts.join("");  })().catch(error => { result.textContent = "Error: " + error.name; });</script>
HTML Reference