The forEach() method of a NodeList calls your callback once for each node, in insertion order. Learn the callback parameters (currentValue, currentIndex, listObj), optional thisArg, and how it compares with entries() and for...of—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
undefined
03
Callback
node, index, list
04
Optional
thisArg
05
Order
Insertion order
06
Status
Baseline widely
Fundamentals
Introduction
When you have a NodeList (from childNodes or querySelectorAll), forEach() is the callback-style way to visit every node. It feels familiar if you already know Array.forEach().
MDN’s classic idea: build a small tree, then call list.forEach(callback, thisArg) to log each node, its index, and the this value.
💡
Beginner tip
forEach always walks the whole list—there is no break. Need to stop early? Use for...of or entries().
Concept
Understanding the forEach() Method
An instance method on NodeList that executes a callback for each node in the list, in insertion order.
NodeList.forEach() is marked Baseline Widely available on MDN (since October 2017). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
NodeList.forEach()
Runs a callback for each node in insertion order. Optional thisArg sets this for non-arrow callbacks.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerNo NodeList.forEach() (use a polyfill or indexed for loop)
Unsupported
NodeList.forEach()Excellent
Bottom line: Use list.forEach(callback) on childNodes or querySelectorAll results for simple per-node side effects.
Wrap Up
Conclusion
NodeList.forEach() is the callback-style walker for every node in a list. Use it for simple side effects; switch to entries() or for...of when you need iterator control or early exit.
Prefer querySelectorAll when you only want elements
Remember text nodes appear in childNodes
❌ Don’t
Expect a return array from forEach
Rely on break inside forEach
Expect arrow functions to honor thisArg
Assume every node is an Element
Use forEach when you need early exit (use for...of)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about NodeList.forEach()
A callback for every node, in order.
5
Core concepts
🔗01
Returns
undefined
API
⚙️02
Callback
node, i, list
Args
🔒03
thisArg
optional
Rule
📄04
No break
full walk
Gotcha
🎯05
Baseline
since Oct 2017
Status
❓ Frequently Asked Questions
It calls your callback once for each node in the NodeList, in insertion order. The callback receives currentValue (the node), currentIndex, and the list itself.
No. MDN marks NodeList.forEach() as Baseline Widely available (since October 2017). It is not Deprecated, Experimental, or Non-standard.
undefined. It runs side effects in the callback; it does not build a new list.
An optional second argument used as this inside the callback when you pass a regular function (not useful with arrow functions, which do not bind their own this).
forEach() runs a callback immediately for each node. entries() returns a lazy iterator of [index, node] pairs for for...of or Array.from.
No convenient break. Use for...of (optionally with entries()) or a classic for loop when you need to stop early.
Did you know?
MDN’s example passes "myThisArg" as the second argument so this inside the callback prints that string. Arrow functions would not receive it that way—use a regular function when you need thisArg.