JavaScript NodeIterator pointerBeforeReferenceNode Property

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

What You’ll Learn

The pointerBeforeReferenceNode property of a NodeIterator is a read-only boolean that tells you whether the walk pointer sits before or after the anchor (referenceNode). Learn what true and false mean, how nextNode() updates the flag, and how to pair it with filter—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

Boolean

03

true

Before anchor

04

false

After anchor

05

Pair

referenceNode

06

Status

Baseline widely

Introduction

A NodeIterator does not only return nodes from nextNode()—it also tracks where the walk is anchored. The read-only pointerBeforeReferenceNode property answers a simple question: is the iterator pointer immediately before the reference node, or immediately after it?

MDN’s idea: after you create an iterator, read nodeIterator.pointerBeforeReferenceNode to inspect that flag. As you call nextNode() or previousNode(), both this boolean and referenceNode can change.

💡
Beginner tip

For everyday loops you often only need nextNode(). Use pointerBeforeReferenceNode when you want to debug iterator position or understand anchor state alongside referenceNode.

Understanding the pointerBeforeReferenceNode Property

A read-only instance property on NodeIterator that describes iterator position relative to the anchor node.

  • Returns — a boolean.
  • true — pointer is before referenceNode.
  • false — pointer is after referenceNode.
  • Read-only — updated by iterator movement, not by assignment.
  • Baseline Widely available on MDN (since April 2018).

📝 Syntax

Read the property on a NodeIterator:

JavaScript
nodeIterator.pointerBeforeReferenceNode

Return value

A boolean: true (before anchor) or false (after anchor).

Typical pattern (MDN idea)

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

const flag = nodeIterator.pointerBeforeReferenceNode;
// true or false depending on anchor position

⚡ Quick Reference

GoalCode / note
Read flagiterator.pointerBeforeReferenceNode
Before anchortrue
After anchorfalse
Anchor nodeiterator.referenceNode
Writable?No (read-only)
MDN statusBaseline Widely available (since April 2018)

🔍 At a Glance

Four facts to remember about pointerBeforeReferenceNode.

Type
boolean

Before/after

Writable?
no

Read-only

Partner
referenceNode

Anchor

Baseline
widely

Since Apr 2018

Examples Gallery

Examples follow MDN NodeIterator.pointerBeforeReferenceNode. Labs show how the flag changes as you walk the tree.

📚 Getting Started

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

Example 1 — Read on a New Iterator (MDN Idea)

Inspect pointerBeforeReferenceNode right after creation.

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

console.log(nodeIterator.pointerBeforeReferenceNode);
// often true before the first nextNode()
Try It Yourself

How It Works

On a fresh iterator, the pointer often starts before the anchor node.

Example 2 — After nextNode()

Compare the flag before and after moving forward once.

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

console.log("before", it.pointerBeforeReferenceNode);
it.nextNode();
console.log("after", it.pointerBeforeReferenceNode);
Try It Yourself

How It Works

nextNode() advances the walk and usually flips the flag to false (after the new anchor).

📈 Anchor & Walk State

Pair with referenceNode, step backward, and log a walk.

Example 3 — Pair With referenceNode

Log anchor name and pointer position together.

JavaScript
function logState(it, label) {
  console.log(
    label,
    it.referenceNode.nodeName,
    it.pointerBeforeReferenceNode,
  );
}

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

logState(it, "start");
it.nextNode();
logState(it, "step1");
Try It Yourself

How It Works

The boolean describes position relative to whatever node referenceNode currently points at.

Example 4 — previousNode() Reverses Direction

Step forward, then backward, and watch the flag change again.

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

it.nextNode();
console.log("after next", it.pointerBeforeReferenceNode);
it.previousNode();
console.log("after previous", it.pointerBeforeReferenceNode);
Try It Yourself

How It Works

Moving backward with previousNode() can restore a “before anchor” state.

Example 5 — Log Flag Through a Short Walk

Collect flag + node name for each nextNode() step.

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(node.textContent + " " + it.pointerBeforeReferenceNode);
}
Try It Yourself

How It Works

After each accepted nextNode(), the flag usually reads false (pointer after the returned node).

🚀 Common Use Cases

  • Debug iterator position during complex DOM walks.
  • Inspect anchor state alongside referenceNode.
  • Verify whether the pointer is before or after the current anchor.
  • Learn how nextNode() and previousNode() move the walk.
  • Build advanced traversal tools that depend on exact iterator state.

🔧 How It Works

1

Create iterator

document.createNodeIterator sets an initial referenceNode anchor.

Create
2

Read flag

pointerBeforeReferenceNode is true if the pointer is before that anchor.

Property
3

Move walk

nextNode() or previousNode() updates referenceNode and the boolean.

Move
4

Interpret state

true = before anchor; false = after anchor—pair with referenceNode for full context.

📝 Notes

  • MDN: Baseline Widely available (since April 2018) — no Deprecated / Experimental / Non-standard banner.
  • Read-only boolean; movement methods update it.
  • Works together with referenceNode to describe iterator state.
  • Most simple loops only need nextNode(); this property is for position awareness.
  • Related learning: filter, NodeList.forEach(), childNodes.

Universal Browser Support

NodeIterator.pointerBeforeReferenceNode 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.pointerBeforeReferenceNode

Read-only boolean: true when the iterator pointer is before referenceNode, false when after.

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
pointerBeforeReferenceNode Excellent

Bottom line: Read this flag with referenceNode to understand where a NodeIterator is anchored in the tree.

Conclusion

NodeIterator.pointerBeforeReferenceNode tells you whether the walk pointer is before (true) or after (false) the anchor node. Read it with referenceNode to understand iterator state, especially after nextNode() or previousNode().

Continue with referenceNode, filter, NodeList.forEach(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read it alongside referenceNode for full context
  • Use it when debugging iterator walks
  • Remember true = before, false = after
  • Expect the flag to change when you call movement methods
  • Prefer simple nextNode() loops for basic tasks

❌ Don’t

  • Try to assign a value to pointerBeforeReferenceNode
  • Confuse it with filter (that screens nodes)
  • Assume it stays constant during a walk
  • Rely on it when querySelectorAll is enough
  • Forget that referenceNode moves with the iterator

Key Takeaways

Knowledge Unlocked

Five things to remember about pointerBeforeReferenceNode

Before or after the anchor.

5
Core concepts
⚙️02

true

before

Position
🔒03

false

after

Position
📄04

+ ref node

pair

Context
🎯05

Baseline

since Apr 2018

Status

❓ Frequently Asked Questions

A read-only boolean. true means the iterator pointer sits before referenceNode; false means it sits after referenceNode.
No. MDN marks it as Baseline Widely available (since April 2018). It is not Deprecated, Experimental, or Non-standard.
No. It is read-only. Call nextNode() or previousNode() to move the iterator; the flag updates automatically.
referenceNode is the anchor node. pointerBeforeReferenceNode tells you whether the walk position is immediately before or after that anchor.
Yes. Moving forward with nextNode() (or backward with previousNode()) updates both referenceNode and this boolean as the iterator advances.
Usually not for simple walks. It helps when debugging iterator state or building advanced traversal logic that depends on exact anchor position.
Did you know?

MDN’s sample reads nodeIterator.pointerBeforeReferenceNode right after creation. The interesting part is watching it change as you call nextNode() and previousNode()—together with referenceNode.

Explore referenceNode

Next up: read the anchor Node the iterator is tied to.

referenceNode 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