JavaScript Node parentNode Property

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is parentNode?

node.parentNode returns the parent Node of a node in the DOM tree, or null if there is no parent (MDN). The parent may be an Element, Document, or DocumentFragment. Use parentElement when you need an Element-only parent.

Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Read-only — change structure with append/remove APIs, not by assigning here.

Remember
node.parentNode
→ parent Node (Element | Document | DocumentFragment | …)
→ or null (no parent)

always null:
  document.parentNode
  DocumentFragment.parentNode
  freshly created, unattached nodes

parentNode    → any parent type
parentElement → Element only (else null)

Classic remove:
  if (node.parentNode) {
    node.parentNode.removeChild(node);
  }
GoalPrefer
Any parent nodenode.parentNode
Parent tag (Element)node.parentElement
Owning Documentnode.ownerDocument

How to Use parentNode

Read the parent Node, null-check, then act (inspect, remove, climb).

  1. Select a node (element, text, …).
  2. Read node.parentNode.
  3. If it is not null, use it (e.g. removeChild, inspect nodeName).
  4. Use parentElement when you need Element APIs like style.
PieceRequired?Role
nodeYesAny DOM Node
Return—Parent Node, or null
Null checkUsuallyAvoid calling methods on null

Pseudocode

Pseudocode
parent = node.parentNode

if parent is null:
  // detached, or Document / DocumentFragment
else:
  // parent may be Element, Document, …
  parent.removeChild(node)   // classic remove

// vs parentElement:
html.parentNode    → document
html.parentElement → null

document.parentNode → null   // never has a parent

Cheat Sheet

Keep these facts handy when walking up the DOM.

TopicDetail
Syntaxnode.parentNode
ReturnsNode or null
Mutates?No — read-only
vs parentElementAny parent vs Element-only
document / FragmentAlways null (no parent)
Detached nodenull (not attached yet)
StandardBaseline Widely available (since July 2015)

Live Preview

Read parentNode, compare with parentElement, or run the MDN remove pattern.

Demo
Hi

Starts from #pn-child. Remove uses MDN’s parentNode.removeChild pattern (re-adds after).

Result
Choose an action, then Run…

JavaScript Examples

Short, copy-ready snippets based on MDN Node.parentNode. Use Try It Yourself for a live editor.

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

  1. Select the child. Nested inside a div.
  2. Read parentNode. Returns that wrapping Node.
  3. Inspect fields. id and nodeName work on the Element parent.
  4. Same as parentElement here because the parent is an Element.

Example 2 — Remove via parentNode (MDN)

MDN’s classic: remove a node only if it already has a parent.

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

if (node.parentNode) {
  node.parentNode.removeChild(node);
  console.log("Removed via parentNode");
}

console.log(node.parentNode); // null after remove
Try It Yourself

How It Works

  1. Guard with if. Skip if the node is already detached.
  2. Ask the parent to remove. removeChild needs the parent.
  3. Matches MDN’s sample for safe removal.
  4. Modern alternative: node.remove() when you do not need the parent pointer.

Example 3 — HTML Root: parentNode vs parentElement

The document root’s parent is the Document — a Node, not an Element.

JavaScript
const html = document.documentElement;

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

How It Works

  1. html’s parent is Document. That is still a Node.
  2. parentNode returns it. You can see the Document.
  3. parentElement filters to Elements. Result is null.
  4. Pick the API based on whether you need Document parents.

Example 4 — Document Never Has a Parent

MDN: Document and DocumentFragment always return null.

JavaScript
const frag = document.createDocumentFragment();

console.log(document.parentNode); // null
console.log(frag.parentNode);     // null
console.log(frag.nodeName);       // "#document-fragment"
Try It Yourself

How It Works

  1. Document is the root. It has no parent Node.
  2. Fragments are the same. They never sit under a parent.
  3. Both return null. By design, not an error.
  4. Children inside a fragment do have the fragment as parentNode until moved.

Example 5 — Detached Nodes Have No Parent

Created but not appended elements return null until attached.

JavaScript
const orphan = document.createElement("div");

console.log(orphan.parentNode);  // null
console.log(orphan.isConnected); // false

document.body.appendChild(orphan);
console.log(orphan.parentNode === document.body); // true
Try It Yourself

How It Works

  1. createElement alone. Node is not in the tree.
  2. parentNode is null. No parent slot yet.
  3. Append to attach. Parent becomes body.
  4. isConnected flips when the node joins a live document.

Edge Cases & Pitfalls

Check these before assuming every node has a parent.

null

Document / Fragment parents are always null

MDN: these node types never have a parent. Do not treat that as a bug.

detached

Unappended nodes have no parent

Fresh createElement results return null until you append them.

vs

parentElement is narrower

Prefer parentElement for Element APIs; use parentNode when Document / Fragment parents matter.

guard

Null-check before removeChild

Follow MDN: if (node.parentNode) { node.parentNode.removeChild(node); }.

Browser Support

Node.parentNode is marked Baseline Widely available on MDN (since July 2015).

✓ Baseline · Widely available

Node.parentNode

Returns any parent Node or null. Broader than parentElement. Classic removeChild partner. Safe everyday DOM API.

100% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
parentNode Excellent

Bottom line: Safe everyday DOM API. Remember Document → null. See the MDN parentNode reference.

Performance Notes

ApproachCostBest for
node.parentNodeO(1)One step up the tree
Climb with whileO(depth)Ancestor walks (any Node)
node.remove()O(1)Remove without reading parent

One parent read is cheap. Prefer node.remove() when you only need to detach and do not care about the parent pointer.

Key Takeaways

  • Any parent Node: or null if none.
  • vs parentElement: any parent type vs Element-only.
  • Document / Fragment → null: they never have a parent.
  • Null-check: then removeChild (or use node.remove()).

One line: parentNode is the parent in the tree — any Node type — or null when there is none.

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

Next: previousSibling

Walk sideways to the previous sibling node in the tree.

previousSibling tutorial →

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.

9 people found this page helpful