JavaScript Node parentNode Property

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

What You’ll Learn

The Node.parentNode property returns the parent of a node in the DOM tree—or null if there is none. Learn parentNode vs parentElement, the classic removeChild pattern, and when Document parents appear.

01

Kind

Read-only property

02

Returns

Node or null

03

May be

Element / Document

04

Document

Always null

05

Classic use

removeChild

06

Status

Baseline widely

Introduction

parentNode answers: “Who is above me in the tree?” That parent can be an Element (most UI cases), a Document (parent of <html>), or a DocumentFragment.

When you only want a tag parent for styling, prefer parentElement. When you need the full story—including Document—use parentNode.

💡
Beginner tip

The classic remove idiom is if (node.parentNode) node.parentNode.removeChild(node). In modern browsers you can also write node.remove().

Understanding parentNode

MDN: the read-only parentNode property returns the parent of the specified node in the DOM tree. Document and DocumentFragment nodes never have a parent, so their parentNode is always null. Newly created, unattached nodes also return null.

  • Parent is usually an Element, Document, or DocumentFragment.
  • Returns null when there is no parent.
  • Read-only — mutate the tree with DOM methods.

📝 Syntax

JavaScript
const parent = node.parentNode;
if (parent) {
  parent.removeChild(node);
}

Return value

A Node that is the parent, or null if there is none.

⚖️ parentNode vs parentElement

node.parentNodenode.parentElement
ReturnsAny parent Node (or null)Parent Element only (or null)
Child of a divThat divThat div
<html>documentnull
Detached nodenullnull
Best forFull tree / removeChildUI Element parents

⚡ Quick Reference

GoalCode
Get parentnode.parentNode
Remove self (classic)node.parentNode?.removeChild(node)
Remove self (modern)node.remove()
Element parent onlynode.parentElement
<html> parentdocument.documentElement.parentNodedocument
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Node.parentNode.

Returns
Node | null

Any parent

Baseline
widely

Since July 2015

Access
read-only

No assignment

Document / fragment
null

Never have parents

Examples Gallery

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

📚 Getting Started

Read the parent and remove a node safely.

Example 1 — Read the Parent Node

A child inside a wrapper reports that wrapper as parentNode.

JavaScript
const child = document.getElementById("child");
const parent = child.parentNode;

console.log(parent.id);       // "wrap"
console.log(parent.nodeName); // "DIV"
Try It Yourself

How It Works

For an Element parent, parentNode and parentElement usually match. The difference shows up at the document root.

Example 2 — Remove via parentNode (MDN-style)

Only call removeChild when a parent exists.

JavaScript
const node = document.getElementById("child");

if (node.parentNode) {
  node.parentNode.removeChild(node);
}

console.log(document.getElementById("child")); // null
Try It Yourself

How It Works

The guard skips work when the node is already detached. After removal, parentNode becomes null.

📈 Document Parents, null & Climbing

See Document as a parent, know who never has one, walk upward.

Example 3 — <html> Parent Is the Document

This is where parentNode and parentElement diverge.

JavaScript
const html = document.documentElement;

console.log(html.parentNode);              // document
console.log(html.parentNode.nodeName);     // "#document"
console.log(html.parentElement);           // null
Try It Yourself

How It Works

The Document is a Node but not an Element. parentNode returns it; parentElement does not.

Example 4 — Who Always Has null?

Documents, fragments, and detached nodes have no parent.

JavaScript
console.log(document.parentNode); // null

const frag = document.createDocumentFragment();
console.log(frag.parentNode);     // null

const orphan = document.createElement("div");
console.log(orphan.parentNode);   // null
Try It Yourself

How It Works

Per MDN, Document and DocumentFragment never have parents. Unattached created nodes also report null until appended.

Example 5 — Climb Including the Document

Walk parentNode until null—you will see #document.

JavaScript
let n = document.getElementById("child");
const path = [];

while (n) {
  path.push(n.nodeName);
  n = n.parentNode;
}

console.log(path.join(" → "));
// e.g. SPAN → DIV → BODY → HTML → #document
Try It Yourself

How It Works

Unlike a parentElement climb (which stops at HTML), this walk includes the Document before ending at null.

🚀 Common Use Cases

  • Safe removalparentNode.removeChild(node) when attached.
  • Full ancestor walks — include Document in the path.
  • Fragment trees — parents can be DocumentFragment during builds.
  • Feature detection of attachment!!node.parentNode (or use isConnected).
  • Compare with parentElement — teach Document vs Element parents.

🧠 How parentNode Works

1

Look for a parent link

Attached nodes point to the node that contains them.

Link
2

Return that Node

Element, Document, or DocumentFragment in normal cases.

Parent
3

Or return null

No parent yet, or the node is Document / DocumentFragment.

Null
4

Your script branches

Remove, climb, or fall back when there is no parent.

📝 Notes

Universal Browser Support

Node.parentNode 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.parentNode

Safe for production. Use parentNode for any parent type; use parentElement when you only want an Element.

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
parentNode Excellent

Bottom line: Use parentNode for full parent access and classic removeChild; use parentElement for Element-only UI parents.

Conclusion

Node.parentNode returns any parent Node or null. Use it for full tree walks and the classic remove pattern; switch to parentElement when you need an Element for styling APIs.

Continue with previousSibling, parentElement, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Null-check before removeChild
  • Use parentNode when Document parents matter
  • Prefer parentElement for Element-only UI work
  • Consider node.remove() for simple detach
  • Pair with isConnected when teaching attachment

❌ Don’t

  • Assume every node has a parent
  • Assign to parentNode (read-only)
  • Call removeChild without a parent guard
  • Expect Document or DocumentFragment to have parents
  • Confuse DOM Node with the Node.js runtime

Key Takeaways

Knowledge Unlocked

Five things to remember about parentNode

Any parent Node—or null.

5
Core concepts
⚖️ 02

vs parentElement

broader

Compare
🗑️ 03

removeChild

classic pattern

Pattern
📄 04

html → document

#document

Root
🚫 05

Doc / fragment

always null

Gotcha

❓ Frequently Asked Questions

The parent Node in the DOM tree — typically an Element, Document, or DocumentFragment — or null if there is no parent.
No. MDN marks Node.parentNode as Baseline Widely available (since July 2015). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
parentNode returns any parent node type. parentElement returns only an Element, or null when the parent is a Document (or there is no parent).
Document and DocumentFragment nodes never have a parent, so parentNode is always null for them. Newly created, unattached nodes also return null.
Classic pattern: if (node.parentNode) { node.parentNode.removeChild(node); }. Modern code often uses node.remove() instead.
Yes. You read it to find the parent; you change structure with appendChild, removeChild, remove, and similar methods.
Did you know?

MDN’s remove example is still the cleanest mental model for beginners: only ask the parent to remove you if you have a parent. That single if (node.parentNode) guard prevents a common runtime error on detached nodes.

Next: previousSibling

Read the previous sibling node—or null at the start of the list.

previousSibling →

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