JavaScript Node previousSibling Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Read-only

What You’ll Learn

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

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).

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.
  • Opposite direction: nextSibling.
  • Read-only — you do not assign to previousSibling.

📝 Syntax

JavaScript
const prev = node.previousSibling;
if (prev) {
  console.log(prev.nodeName);
}

Return value

A Node, or null if there is no previous sibling.

⚖️ previousSibling vs previousElementSibling

node.previousSiblingelement.previousElementSibling
InterfaceNodeElement
ReturnsAny previous sibling node (or null)Previous element sibling (or null)
WhitespaceOften #textSkips text / comments
Best forFull DOM / text-aware walksMost UI sibling work

⚡ Quick Reference

GoalCode
Get previous siblingnode.previousSibling
Safe readnode.previousSibling?.nodeName
Previous element onlyelement.previousElementSibling
Opposite directionnode.nextSibling
Walk backwardwhile (el) { …; el = el.previousSibling; }
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Node.previousSibling.

Returns
Node | null

Previous sibling

Baseline
widely

Since July 2015

Access
read-only

No assignment

Watch for
#text

Between tags

Examples Gallery

Examples follow MDN Node.previousSibling patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See how source whitespace changes previousSibling.

Example 1 — Compact Markup → Element (MDN-style)

Adjacent tags with no whitespace: the previous sibling is the prior element.

JavaScript
// <span id="b0"></span><span id="b1"></span><span id="b2"></span>

const b1 = document.getElementById("b1");
console.log(b1.previousSibling.id); // "b0"

const b2 = document.getElementById("b2");
console.log(b2.previousSibling.id); // "b1"
Try It Yourself

How It Works

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"
Try It Yourself

How It Works

MDN notes that calling .id on a text sibling returns undefined—text nodes do not have an id.

📈 previousElementSibling, null & Walking

Skip text nodes, handle the start of the list, loop backward.

Example 3 — previousSibling vs previousElementSibling

Skip whitespace by asking for the previous element sibling.

JavaScript
const b2 = document.getElementById("b2");
console.log(b2.previousSibling.nodeName);            // often "#text"
console.log(b2.previousElementSibling.nodeName);     // "SPAN"
console.log(b2.previousElementSibling.id);           // "b1"
Try It Yourself

How It Works

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");
}
Try It Yourself

How It Works

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);
Try It Yourself

How It Works

Each step moves one node earlier in the parent’s child list. The loop stops when previousSibling becomes null.

🚀 Common Use Cases

  • Backward sibling walks — list or process every node before a start point.
  • Teach whitespace — show why indented HTML creates #text between tags.
  • Prefer elements for UI — switch to previousElementSibling for tags.
  • Pair with nextSibling — move both directions in childNodes.
  • Custom list logic — step to the prior item without re-querying the DOM.

🧠 How previousSibling Works

1

Find the parent list

Siblings live in the same parent’s childNodes.

Parent
2

Step one backward

Return the node immediately before this one, any type.

Previous
3

Or return null

If this node is first (or has no parent), the value is null.

Start
4

Your script branches

Check for null, then read nodeName or keep walking.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Whitespace and comments can be the previous sibling.
  • Use previousElementSibling when you only want elements.
  • Related: nextSibling, textContent, parentNode, firstChild, JavaScript hub.

Universal Browser Support

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.

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 & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-standing support in legacy IE
Full support
previousSibling Excellent

Bottom line: Read previousSibling for any previous node type; use previousElementSibling when you only want an element.

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.

Continue with textContent, nextSibling, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Null-check previousSibling before using it
  • Use previousElementSibling for element-only needs
  • Expect #text between pretty-printed 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

Key Takeaways

Knowledge Unlocked

Five things to remember about previousSibling

Previous sibling node—or null.

5
Core concepts
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.

Next: textContent

Read or write plain text for a node and its descendants.

textContent →

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