JavaScript Element previousElementSibling Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property

What You’ll Learn

Element.previousElementSibling is a read-only instance property that returns the element immediately before the current one in the parent’s children list, or null when there is no previous element. Learn how it differs from previousSibling, how to chain backward traversal, and how to use it safely—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read-only

03

Type

Element or null

04

Skips

Text & comments

05

Status

Baseline widely

06

Pairs with

nextElementSibling

Introduction

When you already have one element and need the tag just before it, previousElementSibling is the direct read.

It is perfect for stepping backward across cards, list items, rows, or menu entries without getting tripped up by whitespace text nodes that previousSibling may return.

JavaScript
const third = document.getElementById("div-03");
console.log(third.previousElementSibling.textContent);
💡
Beginner tip

Need the previous node of any type, including text? Use previousSibling. Need only the previous tag? Use previousElementSibling.

Understanding the Property

MDN: the read-only previousElementSibling property returns the Element immediately prior to the specified one in its parent’s children list, or null if the specified element is the first one in the list.

  • Read-only — use it to navigate, not assign.
  • Element or null — never returns text or comment nodes.
  • Sibling-only — stays on the same parent level.
  • Baseline — widely available since July 2015 (MDN).

📝 Syntax

JavaScript
previousElementSibling

Value

An Element reference for the previous sibling element, or null when there is no preceding element sibling.

ItemDetail
TypeElement or null
AccessRead-only property
RelatedpreviousSibling, nextElementSibling, children
ScopeOnly among siblings with the same parent
⚠️
Null safety

If the current element is the first one, previousElementSibling is null. Check before reading properties like tagName or textContent.

📋 MDN Backward Loop Shape

MDN starts from one element and walks backward through preceding element siblings:

JavaScript
let el = document.getElementById("div-03").previousElementSibling;
console.log("Siblings of div-03:");
while (el) {
  console.log(el.nodeName);
  el = el.previousElementSibling;
}

Related learning: previousSibling, nextElementSibling, and children.

⚡ Quick Reference

GoalCode / note
Previous element siblingel.previousElementSibling
Previous node (any type)el.previousSibling
Next element siblingel.nextElementSibling
Start checkel.previousElementSibling === null
Walk backwardwhile (el) el = el.previousElementSibling
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.previousElementSibling.

Kind
get only

Instance

Type
Element|null

Or null

Moves
backward

Same parent

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: previousElementSibling. Labs use simple sibling groups so you can compare element-only navigation safely.

📚 Getting Started

Read the previous sibling element like MDN’s sibling example.

Example 1 — Read the Previous Element

Move from div-03 back to the previous sibling element.

JavaScript
const last = document.getElementById("div-03");
console.log(last.previousElementSibling.id);
Try It Yourself

How It Works

previousElementSibling skips straight to the previous element at the same level under the same parent.

📈 Compare, Start & Traversal

See the previousSibling difference, null cases, and backward chaining.

Example 2 — vs previousSibling

Whitespace text nodes may appear in previousSibling but not in previousElementSibling.

JavaScript
const second = document.getElementById("second");
console.log({
  previousSiblingType: second.previousSibling.nodeName,
  previousElementSiblingTag: second.previousElementSibling.tagName
});
Try It Yourself

How It Works

Pretty-printed HTML often inserts text nodes between siblings. Prefer previousElementSibling when you need the previous tag, not whitespace.

Example 3 — null at the Start

The first element sibling has no previous element.

JavaScript
const first = document.getElementById("first");
console.log(first.previousElementSibling); // null
console.log(first.previousSibling === null || first.previousSibling.nodeName === "#text");
Try It Yourself

How It Works

Once the current element is already the first element sibling, previousElementSibling becomes null.

Example 4 — Chain Through Siblings Backward

Walk backward across element siblings the same way MDN demonstrates.

JavaScript
let el = document.getElementById("div-03").previousElementSibling;
const names = [];
while (el) {
  names.push(el.nodeName);
  el = el.previousElementSibling;
}
console.log(names.join(", "));
Try It Yourself

How It Works

Each read moves one sibling backward. Repeating it in a loop is a simple way to walk the preceding element siblings.

Example 5 — Support Snapshot

Feature-detect and remember the sibling-only tip.

JavaScript
console.log({
  supported: "previousElementSibling" in Element.prototype,
  returns: "Element or null",
  tip: "Use previousSibling only when you need text/comment nodes too",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Safe to use widely. It is the cleaner navigation choice when you only care about element siblings.

🚀 Common Use Cases

  • Moving to the previous tab, card, row, or menu item element.
  • Skipping whitespace nodes that previousSibling would return.
  • Walking backward through preceding siblings in a loop.
  • Checking whether an element has a preceding sibling before enabling a "back" action.
  • Pairing with nextElementSibling for bidirectional sibling navigation.

🔧 How It Works

1

Current element sits in a sibling list

All children share the same parent.

DOM
2

Browser looks backward

Skips non-element nodes between siblings.

Filter
3

Returns previous Element or null

Null means there is no preceding element sibling.

Read
4

Repeat to keep moving backward

Chaining the property walks across the remaining preceding sibling elements.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Returns null, not undefined, when no previous element sibling exists.
  • From the NonDocumentTypeChildNode mixin, so it is available on element, character data, and document type nodes where applicable.
  • Related: nextElementSibling, outerHTML, children, previousSibling, JavaScript hub.

Browser Support

Element.previousElementSibling is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.previousElementSibling

Read-only — returns the preceding sibling Element or null (skips text/comment nodes).

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported
Yes
previousElementSibling Baseline

Bottom line: Use previousElementSibling to move backward across element siblings. Prefer it over previousSibling when whitespace text nodes would only get in the way.

Conclusion

previousElementSibling is the element-only shortcut to the sibling immediately before the current one. It skips text and comments, returns null at the start, and is perfect for backward sibling navigation.

Continue with nextElementSibling, prefix, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check for null before reading sibling properties
  • Prefer previousElementSibling over previousSibling for tags
  • Use loops for backward traversal across siblings
  • Keep in mind it stays on the same parent level
  • Pair with nextElementSibling for bidirectional navigation

❌ Don’t

  • Assume previousSibling is always an element
  • Skip null checks near the start of sibling lists
  • Assign el.previousElementSibling = ... (read-only)
  • Expect it to move into child elements
  • Use it when you actually need text/comment nodes too

Key Takeaways

Knowledge Unlocked

Five things to remember about previousElementSibling

Read-only Element or null—previous tag sibling only.

5
Core concepts
📝02

Element|null

previous sibling

Type
🔍03

Backward step

same parent

Use
04

Baseline

widely available

Status
🎯05

Skip text

use previousSibling otherwise

Tip

❓ Frequently Asked Questions

It returns the element immediately before the current one in the parent element's children list, or null if there is no preceding element sibling.
No. MDN marks Element.previousElementSibling as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
previousSibling returns the previous node of any type, including text and comment nodes. previousElementSibling skips those and returns only the previous Element.
It returns null when the current element is already the first element sibling.
Yes. You can walk backward across element siblings by reading el.previousElementSibling repeatedly in a loop or step by step.
Yes. It is a read-only property used for navigation, not for assigning a different sibling.
Did you know?

MDN’s looping pattern with el = el.previousElementSibling is one of the simplest ways to walk backward through a sibling row without touching child nodes.

Previous: prefix

Learn how Element.prefix reads the namespace prefix of an element.

← prefix

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