JavaScript Node isConnected Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Boolean

What You’ll Learn

The Node.isConnected property returns a Boolean: whether the node is connected to a Document (directly or indirectly). Learn create-vs-append, Shadow DOM, why Attr is always false, and five examples with try-it labs.

01

Kind

Read-only property

02

Returns

boolean

03

Means

In a Document?

04

Shadow

Counts when linked

05

Attr

Always false

06

Status

Baseline widely

Introduction

Creating a node with document.createElement() does not put it on the page. Until you insert it (or it lives under a connected shadow root), isConnected is false. After a successful insert into the document tree, it becomes true.

That Boolean is useful in components, custom elements, and cleanup logic: “Am I still in the document?”

💡
Beginner tip

Prefer node.isConnected over guessing with document.contains(node) when you only need a yes/no connected check—it also covers Shadow DOM connection correctly.

Understanding isConnected

MDN: the read-only isConnected property returns a boolean indicating whether the node is connected (directly or indirectly) to a Document object.

  • true — the node’s root is a document (including via shadow trees that are connected).
  • false — detached node, fragment-only node, or special cases like Attr.
  • Read-only — you cannot assign node.isConnected = true.
  • Changes when you append, remove, or adopt nodes.

📝 Syntax

JavaScript
const connected = node.isConnected; // true or false

Return value

A boolean: true if connected to its relevant context object (a document), otherwise false.

⚠️ Attr Is Never Connected

An Attr node always returns false for isConnected, even when its ownerElement is in the document. Attributes are associated with elements but are not part of the node tree the same way—an Attr has no parent and is its own root, so it is never considered connected.

⚡ Quick Reference

SituationTypical isConnected
document.createElement("p")false
After document.body.appendChild(el)true
After el.remove() / removeChildfalse
Node inside connected shadow roottrue
Attr nodeAlways false
Child only inside a DocumentFragmentfalse until inserted into a document
MDN statusBaseline Widely available (since January 2020)

🔍 At a Glance

Four facts to remember about Node.isConnected.

Returns
boolean

true / false

Baseline
widely

Since Jan 2020

Access
read-only

No assignment

Means
in Document?

Direct or via shadow

Examples Gallery

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

📚 Getting Started

Create a node, insert it, then detach it again.

Example 1 — Standard DOM Create & Append

MDN-style: disconnected after create, connected after append.

JavaScript
const test = document.createElement("p");
console.log(test.isConnected); // false

document.body.appendChild(test);
console.log(test.isConnected); // true
Try It Yourself

How It Works

The element exists in memory after createElement, but it is not in the document until appendChild (or similar) runs.

Example 2 — Remove → Disconnected Again

Leaving the tree flips the flag back to false.

JavaScript
const el = document.createElement("div");
document.body.appendChild(el);
console.log(el.isConnected); // true

el.remove();
console.log(el.isConnected); // false
Try It Yourself

How It Works

The node object can still exist in a variable after removal; it is just no longer connected to a document.

📈 Shadow DOM, Attr & Fragments

Connection rules beyond a plain body.appendChild.

Example 3 — Shadow DOM

MDN-style: a node becomes connected when appended to a connected shadow root.

JavaScript
const host = document.createElement("div");
document.body.appendChild(host);

const shadow = host.attachShadow({ mode: "open" });
const style = document.createElement("style");
console.log(style.isConnected); // false

shadow.appendChild(style);
console.log(style.isConnected); // true
Try It Yourself

How It Works

The host is in the document, so its open shadow tree is connected. Children of that shadow root report isConnected === true.

Example 4 — Attr.isConnected Is Always false

Even when the owner element is on the page.

JavaScript
const el = document.createElement("a");
el.setAttribute("href", "/docs");
document.body.appendChild(el);

const attr = el.getAttributeNode("href");
console.log(el.isConnected);   // true
console.log(attr.isConnected); // false
console.log(attr.ownerElement === el); // true
Try It Yourself

How It Works

Attributes are linked via ownerElement but are not tree children of the element for isConnected purposes.

Example 5 — DocumentFragment Children

Nodes in a fragment stay disconnected until moved into the document.

JavaScript
const frag = document.createDocumentFragment();
const item = document.createElement("li");
frag.appendChild(item);
console.log(item.isConnected); // false

document.body.appendChild(frag); // moves children into the body
console.log(item.isConnected); // true
Try It Yourself

How It Works

Appending a fragment inserts its children into the target. Those children become connected; the empty fragment itself is not what you keep using afterward.

🚀 Common Use Cases

  • Custom elements — run setup when connected; tear down when not.
  • Avoid double-insert — skip appendChild if already connected.
  • Cleanup guards — do not update DOM if !node.isConnected.
  • Shadow components — confirm styles/slots are in a connected tree.
  • Debugging detach bugs — log isConnected after remove/replace.

🧠 How isConnected Works

1

Find the node’s root

Walk up parents / shadow hosts to the tree root.

Root
2

Is the root a Document?

Connected trees end at a document (including via shadow).

Check
3

Apply special cases

Attr nodes always report false.

Exceptions
4

Return the Boolean

Your script branches on true or false.

📝 Notes

  • Baseline Widely available (MDN, since January 2020).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Read-only Boolean; insertion/removal updates the value.
  • Attr.isConnected is always false.
  • Related: firstChild, lastChild, childNodes, JavaScript hub.

Universal Browser Support

Node.isConnected is Baseline Widely available across modern browsers (MDN: since January 2020). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Node.isConnected

Safe for modern production apps. Use it to detect whether a node is still in a Document (including connected Shadow DOM).

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
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Not supported in IE 11 · use a polyfill if needed
Unavailable
isConnected Excellent

Bottom line: Check isConnected before DOM updates or cleanup. Remember Attr nodes always return false.

Conclusion

Node.isConnected answers one question: is this node tied to a document right now? Use it after create/append/remove, inside shadow trees, and remember that Attr nodes always report false.

Continue with lastChild, firstChild, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check isConnected before expensive DOM work
  • Treat Shadow DOM children as connected when the host is
  • Re-check after async work (node may have been removed)
  • Use it in custom element connect/disconnect logic
  • Remember Attr is a special always-false case

❌ Don’t

  • Assign to isConnected (read-only)
  • Assume createElement nodes are already on the page
  • Expect attribute nodes to report true
  • Forget fragment children start disconnected
  • Confuse DOM Node with the Node.js runtime

Key Takeaways

Knowledge Unlocked

Five things to remember about isConnected

Boolean link to a Document.

5
Core concepts
📤 02

Create false

append true

DOM
🎨 03

Shadow OK

when host linked

Shadow
🔖 04

Attr

always false

Exception
📁 05

Fragments

until inserted

Pattern

❓ Frequently Asked Questions

It is a read-only Boolean. true means the node is connected (directly or indirectly) to a Document. false means it is not in a document tree yet (or was removed).
No. MDN marks Node.isConnected as Baseline Widely available (since January 2020). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
document.createElement("p") starts with isConnected === false. After you append it into the document (for example document.body.appendChild(el)), isConnected becomes true.
Yes. Nodes appended into a shadow root that is itself connected to a document report isConnected === true.
Attribute nodes are not part of the node tree the same way elements are. Even when ownerElement is connected, Attr.isConnected is always false because an Attr has no parent and is its own root.
Not while they live only inside the fragment. They become connected after you insert them into a document (appending the fragment moves its children into the tree).
Did you know?

Custom Elements expose connectedCallback and disconnectedCallback around the same idea. Reading this.isConnected inside those callbacks (or later async work) tells you whether the element is still in the document.

Next: lastChild

Read the last child node—or null when empty.

lastChild →

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