JavaScript NodeIterator

Beginner
8 min read
Updated: Nov 2026
3 examples
Tree walker

Definition and Usage

A NodeIterator walks the nodes under a root in document order. You create one with document.createNodeIterator(root, whatToShow).

Call nextNode() to move forward and previousNode() to move back. Filters such as NodeFilter.SHOW_ELEMENT limit which nodes appear. Prefer a simple loop with querySelectorAll when you only need elements.

This page teaches what NodeIterator is. Method and property guides live under this section.

Create with document.createNodeIterator.

There is no public new NodeIterator() in normal page scripts. Always start from the document method.

JavaScript Syntax

Create and walk an iterator:

JavaScript
const iterator = document.createNodeIterator(
  root,
  NodeFilter.SHOW_ELEMENT
);
let node = iterator.nextNode();
while (node) {
  console.log(node.nodeName);
  node = iterator.nextNode();
}

Core Ideas

Ideas every NodeIterator tutorial builds on:

IdeaExampleRemember
Createdocument.createNodeIteratorFactory on document
Forwarditerator.nextNode()Next match or null
Backiterator.previousNode()Previous match or null
FilterNodeFilter.SHOW_ELEMENTLimit node kinds
rootiterator.rootSubtree being walked
detachiterator.detach()Legacy no-op in modern browsers

JavaScript Examples

Three short programs that show how NodeIterator works.

Example 1 — Create an element iterator

SHOW_ELEMENT skips text and comments.

JavaScript
const iterator = document.createNodeIterator(root, NodeFilter.SHOW_ELEMENT);
console.log(iterator.root === root);

How It Works

  1. createNodeIterator builds the walker.
  2. SHOW_ELEMENT filters to elements.
  3. root matches the node you passed in.

Example 2 — Walk with nextNode

Each call advances. null means the walk is finished.

JavaScript
let node = iterator.nextNode();
console.log(node && node.nodeName);
node = iterator.nextNode();
console.log(node);

How It Works

  1. The first nextNode() returns the first match.
  2. Further calls continue in document order.
  3. null means there are no more nodes.

Example 3 — Step backward

previousNode moves toward the start of the subtree.

JavaScript
iterator.nextNode();
const again = iterator.previousNode();
console.log(again === iterator.root || again != null);

How It Works

  1. Move forward once with nextNode.
  2. previousNode steps back.
  3. Useful when you overshoot and need the prior match.

Next: NodeIterator Guides

Continue with detach, nextNode, and iterator properties.

Look upWhy
detach()Legacy cleanup method
nextNode()Move forward
previousNode()Move backward
rootSubtree being iterated

Full lists: detach().

Frequently Asked Questions

A NodeIterator walks nodes under a root in document order. You create it with document.createNodeIterator and advance with nextNode.
Use querySelectorAll for simple element lists. Use NodeIterator when you need filtered mixed node types or bidirectional walking.
It is a bitmask from NodeFilter (for example SHOW_ELEMENT or SHOW_TEXT) that limits which node kinds the iterator returns.
In modern browsers detach is a no-op kept for compatibility. Older docs asked you to call it when finished.
Open the method pages starting with detach, then Document createNodeIterator examples.

Open detach()

Then practice nextNode and previousNode on a small subtree.

detach() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

12 people found this page helpful