JavaScript NodeIterator referenceNode Property

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

What You’ll Learn

The referenceNode property of a NodeIterator returns the anchor Node the iterator is tied to. Learn how it starts at the walk root, updates with nextNode(), pairs with pointerBeforeReferenceNode, and stays meaningful when the DOM changes—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

Node

03

Role

Anchor

04

Writable?

Read-only

05

Moves

next/previous

06

Status

Baseline widely

Introduction

A NodeIterator walks a DOM subtree, but it always has an anchor: the node stored in referenceNode. That is the node the iterator position is defined relative to.

MDN’s idea: after document.createNodeIterator(...), read nodeIterator.referenceNode to see the anchor. As you call nextNode() or previousNode(), this property updates to reflect the new anchor.

💡
Beginner tip

For simple loops you may only use the value returned by nextNode(). Use referenceNode when you need to inspect or debug where the iterator is anchored in the tree.

Understanding the referenceNode Property

A read-only instance property on NodeIterator that returns the current anchor Node.

  • Returns — a Node object.
  • Read-only — updated by iterator movement, not by assignment.
  • Initial value — typically the root passed to createNodeIterator.
  • DOM changes — MDN: iterator stays anchored to this node as nodes are inserted.
  • Baseline Widely available on MDN (since April 2018).

📝 Syntax

Read the property on a NodeIterator:

JavaScript
nodeIterator.referenceNode

Return value

A Node—the iterator’s anchor.

Typical pattern (MDN idea)

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

const node = nodeIterator.referenceNode;
console.log(node.nodeName); // often "BODY"

⚡ Quick Reference

GoalCode / note
Read anchoriterator.referenceNode
Node nameiterator.referenceNode.nodeName
After walk stepUpdates when you call next/previous
Pair withpointerBeforeReferenceNode
Writable?No (read-only)
MDN statusBaseline Widely available (since April 2018)

🔍 At a Glance

Four facts to remember about NodeIterator.referenceNode.

Returns
Node

Anchor

Writable?
no

Read-only

Starts at
root

Walk root

Baseline
widely

Since Apr 2018

Examples Gallery

Examples follow MDN NodeIterator.referenceNode. Labs show how the anchor changes as you walk the tree.

📚 Getting Started

Read the anchor when the iterator is new (MDN shape).

Example 1 — Read referenceNode on Create (MDN Idea)

Inspect the anchor right after createNodeIterator.

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

console.log(it.referenceNode === root); // true
Try It Yourself

How It Works

On creation, the anchor is usually the root node you passed in.

Example 2 — After nextNode()

The returned node and referenceNode align after a forward step.

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

const first = it.nextNode();
console.log(it.referenceNode === first); // true
Try It Yourself

How It Works

nextNode() advances the walk and updates the anchor to the node you just reached.

📈 Inspect & Walk

Read nodeName, log a walk, and pair with the pointer flag.

Example 3 — Read nodeName From the Anchor

See what kind of node is currently anchored.

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

console.log(it.referenceNode.nodeName); // "DIV"
it.nextNode();
console.log(it.referenceNode.nodeName); // "SPAN" (first child element)
Try It Yourself

How It Works

referenceNode is a real DOM node—you can read any Node property on it.

Example 4 — Log Anchor Through a Walk

Collect referenceNode.nodeName after each nextNode().

JavaScript
const it = document.createNodeIterator(
  document.getElementById("fruit"),
  NodeFilter.SHOW_ELEMENT,
  {
    acceptNode(node) {
      return node.nodeName === "LI"
        ? NodeFilter.FILTER_ACCEPT
        : NodeFilter.FILTER_REJECT;
    },
  },
);

const log = [];
let node;
while ((node = it.nextNode())) {
  log.push(it.referenceNode.textContent);
}
Try It Yourself

How It Works

After each step, referenceNode points at the node you just accepted in the walk.

Example 5 — Pair With pointerBeforeReferenceNode

Log anchor name and pointer position together.

JavaScript
function logAnchor(it) {
  return it.referenceNode.nodeName + " before=" + it.pointerBeforeReferenceNode;
}

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

console.log(logAnchor(it));
it.nextNode();
console.log(logAnchor(it));
Try It Yourself

How It Works

Full iterator state = anchor node + whether the pointer is before or after it.

🚀 Common Use Cases

  • Inspect where a NodeIterator is anchored during debugging.
  • Read properties from the current anchor node mid-walk.
  • Verify the walk root matches referenceNode on create.
  • Pair with pointerBeforeReferenceNode for full position state.
  • Understand iterator behavior when the DOM is mutated during traversal.

🔧 How It Works

1

createNodeIterator

Pass a root node. referenceNode starts anchored there.

Create
2

Read referenceNode

Returns the current anchor Node—read-only.

Property
3

Move walk

nextNode() or previousNode() updates referenceNode to the new anchor.

Move
4

Use the anchor

Inspect nodeName, textContent, or pair with pointerBeforeReferenceNode.

📝 Notes

  • MDN: Baseline Widely available (since April 2018) — no Deprecated / Experimental / Non-standard banner.
  • referenceNode is read-only; movement methods update it.
  • Do not confuse with iterator.root (the original walk root).
  • MDN: iterator stays anchored to referenceNode as nodes are inserted.
  • Related learning: pointerBeforeReferenceNode, filter, childNodes.

Universal Browser Support

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

Baseline · Widely available

NodeIterator.referenceNode

Read-only anchor Node for the iterator. Updates when you call nextNode() or previousNode().

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 Limited legacy support for NodeIterator state properties
Legacy partial
referenceNode Excellent

Bottom line: Read iterator.referenceNode to see which node the walk is anchored to—pair with pointerBeforeReferenceNode for full state.

Conclusion

NodeIterator.referenceNode is the anchor node for the walk. It starts at the root you pass to createNodeIterator, updates as you move with nextNode() or previousNode(), and pairs naturally with pointerBeforeReferenceNode.

Continue with root, filter, pointerBeforeReferenceNode, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read referenceNode when debugging iterator state
  • Pair it with pointerBeforeReferenceNode
  • Use nextNode() return value for simple collection loops
  • Remember the initial anchor is usually the walk root
  • Distinguish referenceNode from iterator.root

❌ Don’t

  • Try to assign a new node to referenceNode
  • Assume it never changes during a walk
  • Confuse it with the node returned by the last nextNode() in all edge cases without testing
  • Use NodeIterator when querySelectorAll is enough
  • Forget MDN’s note about anchoring when the DOM mutates

Key Takeaways

Knowledge Unlocked

Five things to remember about NodeIterator.referenceNode

The anchor node for the walk.

5
Core concepts
⚙️02

Read-only

no assign

Rule
🔒03

Starts

at root

Create
📄04

Moves

next/prev

Walk
🎯05

Baseline

since Apr 2018

Status

❓ Frequently Asked Questions

A read-only Node—the anchor node the iterator is currently tied to. It updates as you move the walk with nextNode() or previousNode().
No. MDN marks NodeIterator.referenceNode as Baseline Widely available (since April 2018). It is not Deprecated, Experimental, or Non-standard.
No. It is read-only. Create a new NodeIterator or call nextNode()/previousNode() to change which node is anchored.
Usually the root node you passed to createNodeIterator—the starting anchor before you walk forward.
referenceNode is the anchor Node. pointerBeforeReferenceNode is a boolean saying whether the walk pointer is before or after that anchor.
MDN notes the iterator remains anchored to the reference node as specified by this property when nodes are inserted—useful for live DOM awareness.
Did you know?

MDN notes that as new nodes are inserted, the iterator remains anchored to referenceNode. That makes this property important when you traverse live DOM trees that can change during the walk.

Explore NodeIterator.root

Next up: read the fixed subtree root that never moves during a walk.

root 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