The Node.previousSibling property returns the previous node in the parent’s childNodes list, or null if none. Learn the whitespace #text gotcha, previousSibling vs previousElementSibling, and how to walk siblings backward.
01
Kind
Read-only property
02
Returns
Node or null
03
May be
#text / comment
04
Prefer
previousElementSibling
05
Start
null
06
Status
Baseline widely
Fundamentals
Introduction
Siblings share the same parent. previousSibling moves one step backward in that parent’s child list. Use nextSibling to move the other way.
The beginner surprise: pretty-printed HTML inserts whitespace text nodes between tags, so previousSibling is often #text instead of the previous element you see.
💡
Beginner tip
For UI work, prefer Element.previousElementSibling. Use previousSibling when you truly need any node type (including text).
Concept
Understanding previousSibling
MDN: the read-only previousSibling property returns the node immediately preceding this one in the parent’s childNodes, or null if this node is the first child.
Can be an element, text node, comment, or other node type.
Whitespace between sibling tags creates #text nodes.
With no text between tags, each previousSibling is the element you expect.
Example 2 — Whitespace Makes #text (MDN-style)
Line breaks between spans insert text nodes.
JavaScript
const b1 = document.getElementById("b1");
console.log(b1.previousSibling.nodeName); // "#text"
// Skip the text node to reach the prior element:
console.log(b1.previousSibling.previousSibling.id); // "b0"
previousElementSibling ignores text and comment nodes and returns the previous element, or null if none exist.
Example 4 — First Sibling Returns null
Always guard before reading properties on previousSibling.
JavaScript
const first = document.getElementById("b0");
console.log(first.previousSibling); // null (if it is the first child)
if (first.previousSibling) {
console.log(first.previousSibling.nodeName);
} else {
console.log("No previous sibling");
}
Reading .nodeName on null throws. Check the result (or use optional chaining) first.
Example 5 — Walk Preceding Siblings
Loop with previousSibling to list every node before a starting element.
JavaScript
let el = document.getElementById("b2").previousSibling;
let i = 1;
let result = "Siblings before b2:\n";
while (el) {
result += `${i}. ${el.nodeName}${el.id ? " #" + el.id : ""}\n`;
el = el.previousSibling;
i++;
}
console.log(result);
Node.previousSibling is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Node.previousSibling
Safe for production. Remember whitespace text nodes between tags, and prefer previousElementSibling for element-only sibling walks.
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 & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
previousSiblingExcellent
Bottom line: Read previousSibling for any previous node type; use previousElementSibling when you only want an element.
Wrap Up
Conclusion
Node.previousSibling returns the previous sibling node or null. Watch for whitespace #text between tags, prefer previousElementSibling for elements, and use a while loop to walk backward through siblings.
Walk with while (el) { …; el = el.previousSibling; }
Pair with nextSibling for both directions
❌ Don’t
Assume previousSibling is always an element
Read .id on a possible text node without checking
Assign to previousSibling (read-only)
Ignore comments that may sit between elements
Confuse DOM Node with the Node.js runtime
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about previousSibling
Previous sibling node—or null.
5
Core concepts
⬅️01
Previous sibling
or null
API
✨02
#text trap
between tags
Gotcha
⚖️03
vs element
previousElementSibling
Compare
🔒04
Null-safe
check first
Safety
🔄05
Walk back
while previousSibling
Pattern
❓ Frequently Asked Questions
The node immediately before this one in the parent’s childNodes list, or null if this node is the first child (or has no parent).
No. MDN marks Node.previousSibling as Baseline Widely available (since July 2015). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
Browsers insert Text nodes for whitespace between tags in the HTML source. So the node before an element is often #text, not the previous visible tag.
Use Element.previousElementSibling when you want the previous element and want to skip text and comment nodes. Prefer it for most UI sibling walks.
previousSibling is null. Always check before reading nodeName or other properties on the result.
Start from a node and loop backward: while (el) { …; el = el.previousSibling; }. Pair with nextSibling to walk the other way.
Did you know?
MDN’s second example shows b2.previousSibling.id // undefined when the previous sibling is #text. That single undefined is a clear signal you hit whitespace instead of an element—reach for previousElementSibling instead.