Favorites

DOM NodeList.forEach() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
NodeList.forEach()
DOM NodeList MethodThis method is supported by all modern browsers.
Updated 28th November 2026

The forEach() method runs a function once for each node in a NodeList, such as the results of querySelectorAll().

Description

The forEach() method of NodeList calls a function for each node in the list, in order, just like the array method. It's the quickest way to apply a change to every element returned by querySelectorAll(). HTMLCollection doesn't have it, so convert those with Array.from() or use for...of.

An optional second argument sets this inside the callback, although arrow functions make that rarely needed. As with the array version, there's no way to stop the loop early except by throwing; use for...of if you need break.

Since the lists returned by querySelectorAll() are static, it's safe to remove or move elements inside the callback. With live lists such as childNodes, removing nodes during the loop can skip items.

Syntax

nodeList.forEach(callback)

Parameters

  • callbackA function called for each node with node, index and the list.

Return Value

None (undefined).

Example

<span class="tag">html</span><span class="tag">css</span><span class="tag">js</span>
document.querySelectorAll('.tag').forEach((tag, i) => {
  tag.textContent = `${i + 1}. ${tag.textContent}`;
  tag.style.marginRight = '8px';
});
console.log(document.body.textContent.trim().replace(/\s+/g, ' '));

Browser Support

The following information will show you the current browser support for the NodeList.forEach() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee