Node.previousSibling is a read-only property that returns the node immediately before this one in the parent’s childNodes list, or null if this node is first (MDN).
Baseline · Widely available: not Deprecated, Experimental, or Non-standard (since July 2015).
Note: browsers insert Text nodes for whitespace in markup. So previousSibling is often #text, not the previous element — use previousElementSibling to skip non-elements (MDN).
Remember
node.previousSibling → previous Node | null
Includes text / comment nodes (not elements only)
Pretty HTML → often #text between tags
Want the previous element?
node.previousElementSibling
Read it on a node, check for null, and decide whether you need any node or only an element.
Get a node that has a parent (for example an element with an id).
Read node.previousSibling.
If the result is null, there is no previous sibling.
If you only want an element, use previousElementSibling instead.
Piece
Required?
Role
Any Node
Yes
Receiver in a parent’s childNodes
Return value
—
Previous sibling Node, or null
Pseudocode
Pseudocode
if node has no parent OR node is first child:
return null
return the node just before this one
in parent.childNodes // may be Element, Text, Comment, …
Reference
Cheat Sheet
Keep these facts handy when walking siblings backward.
Topic
Detail
Syntax
node.previousSibling
Type
Read-only Node or null
First child?
Returns null
Whitespace
Often returns #text between tags
Elements only
previousElementSibling
Opposite direction
nextSibling
Walk loop
while (n) { …; n = n.previousSibling; }
Status (MDN)
Baseline Widely available
Try it
Live Preview
Compare compact vs spaced markup, skip to the previous element, or walk all preceding siblings.
Browser demo of Node.previousSibling — compact HTML, whitespace, element skip, or walk.
Result
Loading…
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Compact HTML (MDN)
No whitespace between tags — previousSibling is the prior element.
1. #text
2. SPAN #b1
3. #text
4. SPAN #b0
(plus any leading whitespace text)
How It Works
Start one step before #b2. First hop is usually #text.
Loop while truthy. Stop at null.
Print nodeName / id. Shows elements and text nodes.
For elements only. Use previousElementSibling in the loop instead.
Edge Cases & Pitfalls
Check these before assuming the previous tag is an element.
whitespace
Pretty HTML inserts #text
Newlines and spaces between tags become Text nodes — a top beginner surprise (MDN).
null
First child returns null
Always check before reading .id or .nodeName on the result.
elements
Need an element? Use previousElementSibling
Skips text and comments so you land on the previous Element (MDN).
live
Tree changes update the link
Insert or remove siblings and the next read of previousSibling reflects the new tree.
Compatibility
Browser Support
Node.previousSibling is Baseline Widely available (MDN, since July 2015). Classic DOM navigation — safe everywhere.
✓ Baseline · Widely available
Node.previousSibling
Supported across Chrome, Firefox, Safari, Edge, Opera, and legacy IE. Returns the previous childNodes entry or null.
99%Browser support
Google ChromepreviousSibling · full support
Full support
Microsoft EdgepreviousSibling · Chromium
Full support
Apple SafaripreviousSibling · full support
Full support
Mozilla FirefoxpreviousSibling · full support
Full support
OperapreviousSibling · full support
Full support
Internet ExplorerpreviousSibling · legacy
Legacy support
Node.previousSiblingExcellent
Bottom line: Use freely. Prefer previousElementSibling when you only want the previous HTML element.
Analysis
Performance Notes
Approach
Cost
Best for
One previousSibling read
Cheap
Neighbor checks
Walk all preceding
O(k) siblings
Short lists
Elements only
Usually fewer hops
previousElementSibling loop
Sibling pointers are O(1) per hop. For long lists of mixed text nodes, prefer previousElementSibling if you only need elements.
Remember
Key Takeaways
Role: previous entry in parent.childNodes, or null.
Whitespace: pretty HTML often makes the previous node #text.
Elements: use previousElementSibling to skip non-elements.
Guard null: first child / no parent → null.
One line:node.previousSibling is the previous childNodes neighbor — often a Text node when HTML is pretty-printed.
Frequently Asked Questions
The node immediately before this one in the parent’s childNodes list, or null if this node is the first in that list (MDN).
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 (MDN).
Use Element.previousElementSibling when you want the previous element and want to skip text and comment nodes. Prefer it for most UI sibling walks (MDN).
previousSibling is null. Always check before reading nodeName, id, or other properties on the result.
Start from a node and loop backward: while (el) { …; el = el.previousSibling; }. Use nextSibling to walk the other way.