JavaScript NodeList forEach() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

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

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().

Understanding the forEach() Method

An instance method on NodeList that executes a callback for each node in the list, in insertion order.

  • Returnsundefined.
  • Callback argscurrentValue, optional currentIndex, optional listObj.
  • Optional thisArg — value used as this in a non-arrow callback.
  • Does not build a new NodeList by itself.
  • Baseline Widely available on MDN (since October 2017).

📝 Syntax

JavaScript
forEach(callback)
forEach(callback, thisArg)

Parameters

  • callback — function run for each node. Receives:
    • currentValue — the current node
    • currentIndex (optional) — the index
    • listObj (optional) — the NodeList itself
  • thisArg (optional) — value to use as this when running callback

Return value

undefined.

Typical pattern (MDN idea)

JavaScript
list.forEach(function (currentValue, currentIndex, listObj) {
  console.log(`${currentValue}, ${currentIndex}, ${this}`);
}, "myThisArg");

⚡ Quick Reference

GoalCode / note
Visit each nodelist.forEach((node) => { ... })
With indexlist.forEach((node, i) => { ... })
With thisArglist.forEach(fn, thisArg)
Return valueundefined
Need early exitUse for...of / entries()
MDN statusBaseline Widely available (since October 2017)

🔍 At a Glance

Four facts to remember about NodeList.forEach().

Returns
undefined

Side effects

Callback
node, i, list

Up to 3 args

Break?
no

Runs full list

Baseline
widely

Since Oct 2017

Examples Gallery

Examples follow MDN NodeList.forEach(). Labs use childNodes and querySelectorAll.

📚 Getting Started

Call forEach the MDN way.

Example 1 — Callback + thisArg (MDN Idea)

Log each node, its index, and the this value from thisArg.

JavaScript
const node = document.createElement("div");
node.appendChild(document.createElement("p"));
node.appendChild(document.createTextNode("hey"));
node.appendChild(document.createElement("span"));

const list = node.childNodes;

list.forEach(function (currentValue, currentIndex, listObj) {
  console.log(`${currentValue}, ${currentIndex}, ${this}`);
}, "myThisArg");
Try It Yourself

How It Works

A regular function receives thisArg as this. Arrow functions would ignore thisArg.

Example 2 — Simple Arrow Callback

Most demos only need the node (and maybe the index).

JavaScript
list.forEach((child, index) => {
  console.log(index, child.nodeName);
});
Try It Yourself

How It Works

Arrow callbacks are shorter when you do not need thisArg.

📈 Selectors & DOM Updates

Use forEach with querySelectorAll and simple DOM changes.

Example 3 — querySelectorAll + forEach

Visit every matched element and read its text.

JavaScript
const items = document.querySelectorAll("li");

items.forEach((el, i) => {
  console.log(i, el.textContent);
});
Try It Yourself

How It Works

Static NodeLists from selectors are a common place to use forEach in everyday scripts.

Example 4 — Add a Class to Each Match

Use the callback to update the DOM for every selected element.

JavaScript
document.querySelectorAll(".item").forEach((el) => {
  el.classList.add("ready");
});
Try It Yourself

How It Works

forEach is ideal for side effects like classes, attributes, or text updates.

Example 5 — forEach() vs entries()

Both expose an index; the looping style differs.

JavaScript
list.forEach((n, i) => {
  console.log("forEach", i, n.nodeName);
});

for (const [i, n] of list.entries()) {
  console.log("entries", i, n.nodeName);
}
Try It Yourself

How It Works

Pick forEach for simple callbacks; pick entries() when you want for...of or early break.

🚀 Common Use Cases

  • Log or inspect every node in childNodes.
  • Update all matches from querySelectorAll.
  • Add classes, attributes, or listeners in bulk.
  • Teach callback-style iteration next to Array forEach.
  • Pass thisArg when a method needs a specific this.

🔧 How It Works

1

Get a NodeList

From childNodes, querySelectorAll, or another DOM API.

DOM
2

Call forEach

Pass a callback (and optional thisArg).

Call
3

Run per node

Each node is visited in insertion order with index and list.

Loop
4

Done

forEach returns undefined after all callbacks finish.

📝 Notes

  • MDN: Baseline Widely available (since October 2017) — no Deprecated / Experimental / Non-standard banner.
  • Returns undefined; takes a required callback and optional thisArg.
  • childNodes includes text nodes—your callback may see #text.
  • No early break; use for...of / entries() when you need to stop.
  • Related learning: entries(), Array.forEach(), childNodes, JavaScript hub.

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer No 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.

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.

Continue with item(), entries(), childNodes, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use forEach for simple per-node side effects
  • Pass (node, index) when you need the position
  • Use a regular function if you need thisArg
  • 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)

Key Takeaways

Knowledge Unlocked

Five things to remember about NodeList.forEach()

A callback for every node, in order.

5
Core concepts
⚙️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.

Next: NodeList.item()

Learn how to get a node by zero-based index.

item() method →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful