The previousNode() method of a NodeIterator returns the previous node in document order and moves the iterator backward. Learn backtracking after nextNode(), null at the first node, and how it updates referenceNode—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
Node or null
03
Params
None
04
Direction
Backward
05
Pair
nextNode()
06
Status
Baseline widely
Fundamentals
Introduction
A NodeIterator usually walks forward with nextNode(). When you need to move back one step, call previousNode(). It returns the node before the current position and shifts the iterator backward in document order.
MDN’s classic pattern: call nextNode() once, then previousNode()—you land on the same node again because you backtracked. At the very start of the set, previousNode() returns null.
💡
Beginner tip
Think of nextNode() and previousNode() as forward and backward buttons. Most loops only need nextNode(), but backtracking helps when you overshoot or need reversible traversal.
Concept
Understanding the previousNode() Method
An instance method on NodeIterator that returns the previous node in the iterator’s set and moves the position backward.
Returns — a Node, or null at the first node.
Parameters — none.
Direction — backward in document order.
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.previousNode()
Parameters
None.
Return value
A Node representing the previous node in the set, or null if the current node is the first one.
MDN: after one nextNode(), previousNode() returns the same node because the iterator moved back one position.
Example 2 — null at the First Node
Before any forward walk, there is no previous node.
JavaScript
const it = document.createNodeIterator(
document.getElementById("box"),
NodeFilter.SHOW_ELEMENT,
);
console.log(it.previousNode()); // null — already at first position
After the forward walk ends at the last node, repeated previousNode() visits nodes in reverse order until the start.
Example 4 — previousNode() Updates referenceNode
Step forward twice, then back once—anchor moves with each call.
JavaScript
const it = document.createNodeIterator(
document.getElementById("box"),
NodeFilter.SHOW_ELEMENT,
);
it.nextNode();
const second = it.nextNode();
it.previousNode();
console.log(it.referenceNode === second); // false — moved back one step
NodeIterator.previousNode() 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.previousNode()
Returns the previous node in document order and moves the iterator backward—or null at the first node.
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.previousNode()Excellent
Bottom line: Call previousNode() to backtrack after nextNode(), or walk backward from the end of a subtree.
Wrap Up
Conclusion
NodeIterator.previousNode() moves the iterator backward in document order. Use it to backtrack after nextNode(), or to walk from the end toward the start. It returns null when you are already at the first node.
Check for null when walking backward from the start
Pair with nextNode() for reversible walks
Read referenceNode when debugging position
Walk to the end with nextNode() before a backward pass
❌ Don’t
Expect a node when already at the first position
Confuse backward iterator steps with DOM node removal
Call deprecated detach() before walking
Use NodeIterator when a simple array reverse would suffice
Mutate the DOM heavily mid-walk without understanding live updates
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about NodeIterator.previousNode()
Backward walk, one node at a time.
5
Core concepts
🔗01
Returns
Node|null
API
⚙️02
Direction
backward
Rule
🚀03
Pair
nextNode()
Walk
📄04
Start?
null
Stop
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
The previous node in document order within the iterator's subtree, or null when the current position is already at the first node in the set.
No. MDN marks NodeIterator.previousNode() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
When the iterator is at the first node in the set—there is no earlier node to return.
previousNode() moves backward in document order; nextNode() moves forward. Both return a Node or null at the ends of the walk.
Yes. Each successful call moves the iterator backward and updates referenceNode (and pointerBeforeReferenceNode).
Yes. MDN's example calls nextNode() then previousNode() to return to the same node—useful for reversible walks.
Did you know?
MDN’s sample calls nextNode() then previousNode() and gets the same node back. That one-step backtrack is the quickest way to see how iterator position moves.