NodeIterator.previousNode() returns the previous node in the iterator’s set and moves the position backward(MDN). When you are already at the first node, it returns null.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call iterator.previousNode. Start with this line.
JavaScript
iterator.previousNode()
Call
What it does
iterator.previousNode()
Primary signature.
iterator.previousNode(...)
Call with the arguments your case needs.
Built into the browser. Call previousNode(...) on a NodeIterator — no import required.
Library declarations
JavaScript
iterator.previousNode()
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
Return Value
Description
*
Previous Node, or null
JavaScript Examples
Example 1 — Backtrack After nextNode()
MDN’s classic pattern: advance, then reverse one step.
JavaScript
const box = document.createElement("div");
box.innerHTML = "<span>A</span>";
const it = document.createNodeIterator(
box,
NodeFilter.SHOW_ELEMENT,
{ acceptNode() { return NodeFilter.FILTER_ACCEPT; } }
);
const current = it.nextNode();
const previous = it.previousNode();
console.log(current.nodeName, previous.nodeName); // DIV DIV
Move forward three times. Land on the last element.
Loop with previousNode(). Assign and test in the while.
Collect names. Order is reverse document order.
Stop on null. You reached the start of the set.
Example 4 — referenceNode Updates
Remember: the first previousNode() after a forward step often returns the same node — call it again to move earlier.
JavaScript
const box = document.createElement("div");
box.innerHTML = "<b>1</b><i>2</i>";
const it = document.createNodeIterator(box, NodeFilter.SHOW_ELEMENT);
it.nextNode(); // DIV
it.nextNode(); // B
it.nextNode(); // I
it.previousNode(); // I (backtrack to same)
it.previousNode(); // B (one earlier)
console.log(it.referenceNode.nodeName); // B
Store the next node. Second call returns the first SPAN.
Backtrack.previousNode() returns that same SPAN.
Strict equality.=== is true — same object, not a copy.
Compatibility
Browser Support
NodeIterator.previousNode() is marked Baseline Widely available on MDN (since July 2015). DOM API — not a Node.js built-in.
✓ Baseline · Widely available
previousNode()
Walk backward through filtered DOM nodes across modern browsers. Stop when the method returns null.
98%Core tag support
Chrome1+
Full support
Edge12+
Full support
Firefox1+
Full support
Safari3+
Full support
Opera✓
Full support
HTML element100% supported
Bottom line: Safe for production. Pair with nextNode() for reversible walks, and skip deprecated detach().
Frequently Asked Questions
The previous node in document order within the iterator’s set, or null when you are already at the first node. It also moves the iterator position backward.
No. MDN marks NodeIterator.previousNode() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
When the current node is the first node in the set — there is no earlier node to return.
Yes. MDN’s classic pattern calls nextNode() then previousNode() to return to the same node — useful for reversible walks.
Yes. Each successful call moves the iterator backward and updates referenceNode (and pointerBeforeReferenceNode).
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.