DOM NodeList.forEach() Method
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,indexand the list.
Return Value
None (undefined).
Example
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.
Desktop





Tablets & Mobile





