JavaScript NodeIterator root Property

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

What You’ll Learn

The root property of a NodeIterator returns the fixed root Node of the subtree being traversed. Learn how it matches the first argument to createNodeIterator, how it differs from referenceNode, and why it never changes during a walk—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

Node

03

Fixed?

Yes

04

Set at

createNodeIterator

05

vs ref

referenceNode

06

Status

Baseline widely

Introduction

When you call document.createNodeIterator(root, ...), you choose where the walk begins and ends. The iterator remembers that node on its read-only root property—the top of the subtree it is allowed to traverse.

MDN’s classic example: if you pass document.body, then nodeIterator.root is document.body. That value stays the same even after you call nextNode() many times.

💡
Beginner tip

root is the fixed boundary; referenceNode is the moving anchor. On a new iterator they may start equal, but only referenceNode changes as you walk.

Understanding the root Property

A read-only instance property on NodeIterator that returns the node passed as the first argument to createNodeIterator.

  • Returns — a Node object.
  • Read-only — never changes for this iterator.
  • Scope — defines the subtree the iterator can traverse.
  • Not the same as referenceNode, which moves during walks.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Read the property on a NodeIterator:

JavaScript
nodeIterator.root

Return value

A Node—the root of the traversal subtree.

Typical pattern (MDN idea)

JavaScript
const nodeIterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

const root = nodeIterator.root;
console.log(root === document.body); // true

⚡ Quick Reference

GoalCode / note
Read rootiterator.root
Same as create argFirst parameter to createNodeIterator
Changes on walk?No (fixed)
Node nameiterator.root.nodeName
Writable?No (read-only)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NodeIterator.root.

Returns
Node

Walk root

Fixed?
yes

Never moves

Writable?
no

Read-only

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NodeIterator.root. Labs show that root stays fixed while you walk the tree.

📚 Getting Started

Read root the MDN way with document.body.

Example 1 — root Is document.body (MDN Idea)

Pass document.body and read it back from the iterator.

JavaScript
const nodeIterator = document.createNodeIterator(
  document.body,
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

console.log(nodeIterator.root === document.body); // true
Try It Yourself

How It Works

MDN’s sample: root is exactly the node you passed to createNodeIterator.

Example 2 — Root a Specific Element

Limit the walk to a #box subtree instead of the whole document.

JavaScript
const box = document.getElementById("box");
const it = document.createNodeIterator(
  box,
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

console.log(it.root.id); // "box"
Try It Yourself

How It Works

Choosing a smaller root keeps traversal scoped to one part of the page.

📈 Fixed vs Moving

root stays put; referenceNode moves during walks.

Example 3 — root Unchanged After nextNode()

Walk forward, but root still points at the original node.

JavaScript
const box = document.getElementById("box");
const it = document.createNodeIterator(
  box,
  NodeFilter.SHOW_ELEMENT,
  { acceptNode: () => NodeFilter.FILTER_ACCEPT },
);

const rootBefore = it.root;
it.nextNode();
it.nextNode();
console.log(it.root === rootBefore); // true
Try It Yourself

How It Works

nextNode() moves referenceNode, not root.

Example 4 — Two Iterators, Two Roots

Each iterator remembers its own fixed root.

JavaScript
const box = document.getElementById("box");
const list = document.getElementById("fruit");

const itBox = document.createNodeIterator(box, NodeFilter.SHOW_ELEMENT);
const itList = document.createNodeIterator(list, NodeFilter.SHOW_ELEMENT);

console.log(itBox.root.id);   // "box"
console.log(itList.root.id);  // "fruit"
Try It Yourself

How It Works

Create separate iterators when you need separate subtree boundaries.

Example 5 — root vs referenceNode

After a walk step, root and referenceNode usually differ.

JavaScript
const box = document.getElementById("box");
const it = document.createNodeIterator(box, NodeFilter.SHOW_ELEMENT);

console.log("start", it.root === it.referenceNode); // often true
it.nextNode();
console.log("after", it.root === it.referenceNode); // false
Try It Yourself

How It Works

root is the fixed boundary; referenceNode tracks where you are in the walk.

🚀 Common Use Cases

  • Verify which subtree an iterator was created for.
  • Scope walks to a section element instead of document.body.
  • Compare iterators by reading each root.
  • Debug walks by checking root vs referenceNode.
  • Teach the difference between fixed boundaries and moving anchors.

🔧 How It Works

1

Pass root node

createNodeIterator(root, whatToShow, filter) stores that node.

Create
2

Read iterator.root

Returns the same Node—read-only, fixed for this iterator.

Property
3

Walk subtree

nextNode() moves referenceNode inside the root subtree; root itself never changes.

Traverse
4

Know your boundary

root tells you exactly which DOM subtree this iterator owns.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • root is read-only and fixed for the iterator’s lifetime.
  • Do not confuse with referenceNode, which moves during walks.
  • On creation, root and referenceNode often start as the same node.
  • Related learning: referenceNode, filter, childNodes.

Universal Browser Support

NodeIterator.root is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

NodeIterator.root

Read-only Node that is the root of what the NodeIterator traverses—set at createNodeIterator time.

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 Supported on NodeIterator (legacy DOM)
Legacy OK
NodeIterator.root Excellent

Bottom line: iterator.root always points at the node you passed to createNodeIterator—it never moves during the walk.

Conclusion

NodeIterator.root is the fixed subtree root you pass to createNodeIterator. It never moves during a walk, unlike referenceNode. Use it to confirm which part of the DOM an iterator covers.

Continue with whatToShow, filter, childNodes, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Pass the smallest useful subtree as root
  • Read iterator.root to verify iterator scope
  • Compare root with referenceNode while learning
  • Create a new iterator for a different subtree
  • Remember root stays fixed after nextNode()

❌ Don’t

  • Try to assign a new value to iterator.root
  • Confuse root with the moving referenceNode
  • Always use document.body when a smaller root works
  • Assume root changes during traversal
  • Forget that each iterator has its own fixed root

Key Takeaways

Knowledge Unlocked

Five things to remember about NodeIterator.root

The fixed subtree boundary.

5
Core concepts
⚙️02

Fixed

never moves

Rule
🔒03

= create arg

first param

Create
📄04

vs ref

moving anchor

Compare
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

A read-only Node—the root of the subtree the iterator traverses. It is the node you passed as the first argument to document.createNodeIterator().
No. MDN marks NodeIterator.root as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. root is read-only and stays fixed for the life of the iterator. Create a new NodeIterator with a different root if you need another subtree.
root is the fixed walk boundary set at creation. referenceNode is the moving anchor that updates when you call nextNode() or previousNode().
No. Walking the tree updates referenceNode and pointerBeforeReferenceNode, but root always points at the original root node.
Pass the smallest subtree you need—e.g. document.getElementById('list') instead of document.body—to limit the walk and keep examples easy to follow.
Did you know?

MDN’s example sets root = nodeIterator.root and gets document.body. That value never changes—even when referenceNode moves forward through child elements.

Explore whatToShow

Learn how the node-type bitmask controls iterator walks.

whatToShow property →

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