The nextNode() method of a NodeIterator returns the next node in document order and advances the iterator. Learn the first-call behavior, null at the end, common while loops, and how it works with filter and whatToShow—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
Node or null
03
Params
None
04
Direction
Forward
05
Updates
referenceNode
06
Status
Baseline widely
Fundamentals
Introduction
After you create a NodeIterator with document.createNodeIterator(root, whatToShow, filter), you walk the subtree by calling nextNode(). Each call moves one step forward in document order.
The first call to nextNode() returns the first node in the set. When there are no more nodes, it returns null—the usual signal to stop looping.
💡
Beginner tip
The classic pattern is while ((node = it.nextNode())) { ... }. Check for null to know when the walk is finished.
Concept
Understanding the nextNode() Method
An instance method on NodeIterator that returns the next node in the iterator’s set and advances the iterator position.
Returns — a Node, or null when done.
Parameters — none.
First call — returns the first node in the set (MDN).
Advances — updates referenceNode and pointerBeforeReferenceNode.
Respects — whatToShow bitmask and filter.acceptNode().
Baseline Widely available on MDN (since July 2015).
Foundation
📝 Syntax
Call the method on a NodeIterator:
JavaScript
nodeIterator.nextNode()
Parameters
None.
Return value
A Node representing the next node in the set, or null if the current node is the last one.
Typical pattern (MDN idea)
JavaScript
const nodeIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
{
acceptNode(node) {
return NodeFilter.FILTER_ACCEPT;
},
},
);
const currentNode = nodeIterator.nextNode(); // first node in the set
Four facts to remember about NodeIterator.nextNode().
Returns
Node|null
Next or done
Params
none
No arguments
Direction
forward
Document order
Baseline
widely
Since Jul 2015
Hands-On
Examples Gallery
Examples follow MDN NodeIterator.nextNode. Labs walk real DOM subtrees with SHOW_ELEMENT and custom filters.
📚 Getting Started
First call and basic forward walks.
Example 1 — First nextNode() Call (MDN Idea)
Create an iterator on document.body and get the first element node.
JavaScript
const nodeIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
{ acceptNode: () => NodeFilter.FILTER_ACCEPT },
);
const currentNode = nodeIterator.nextNode();
console.log(currentNode.nodeName); // e.g. "HTML" or first element in set
MDN: the first nextNode() returns the first node in the iterator’s set, not the second.
Example 2 — while Loop Until null
Keep calling nextNode() until the walk is finished.
JavaScript
const it = document.createNodeIterator(
document.getElementById("box"),
NodeFilter.SHOW_ELEMENT,
);
let count = 0;
let node;
while ((node = it.nextNode())) {
count++;
}
console.log(count); // number of element nodes visited
NodeIterator.nextNode() 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.nextNode()
Returns the next node in document order and advances the iterator—or null when done.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported on NodeIterator (legacy DOM)
Legacy OK
NodeIterator.nextNode()Excellent
Bottom line: Call nextNode() in a while loop to walk a DOM subtree created with createNodeIterator.
Wrap Up
Conclusion
NodeIterator.nextNode() is the main way to walk a DOM subtree forward. The first call returns the first node; later calls advance until null. Combine it with whatToShow and filter for precise traversal.
Combine whatToShow with a custom filter when needed
Read referenceNode when debugging iterator position
❌ Don’t
Assume the first call skips the first node (it does not)
Ignore null and keep calling without checking
Call deprecated detach() before walking
Use NodeIterator when querySelectorAll is enough
Mutate the DOM recklessly mid-walk on live subtrees
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about NodeIterator.nextNode()
Forward walk, one node at a time.
5
Core concepts
🔗01
Returns
Node|null
API
⚙️02
First call
first node
Rule
🚀03
Advances
iterator
State
📄04
Done?
null
Stop
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
The next node in document order within the iterator's subtree, or null when no nodes remain. The first call returns the first node in the set.
No. MDN marks NodeIterator.nextNode() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
When you have reached the end of the walk—there are no more nodes to return in the iterator's set (after whatToShow and filter screening).
Yes. Each successful call advances the iterator and updates referenceNode (and pointerBeforeReferenceNode) to reflect the new position.
nextNode() moves forward in document order; previousNode() moves backward. Both return a Node or null at the ends of the walk.
Old specs said detach() could invalidate the iterator and cause INVALID_STATE_ERR. Modern browsers treat detach() as a no-op and usually do not throw—still avoid detach() in new code.
Did you know?
MDN’s example assigns currentNode = nodeIterator.nextNode() on the first line. That is the standard idiom—the first call already gives you the first node in the set, not an empty step.