JavaScript Element assignedSlot Property

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

What You’ll Learn

Element.assignedSlot is a read-only instance property that returns the HTMLSlotElement an element is inserted into. Learn how Shadow DOM slots work, when the value is null, and how to inspect assignment from JavaScript—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read-only

03

Type

HTMLSlotElement | null

04

Topic

Shadow DOM slots

05

Status

Baseline widely

06

Needs

Open shadow root

Introduction

Custom elements often use slots as placeholders inside a shadow tree. Light-DOM children with a matching slot attribute appear in those placeholders when the page renders.

From a slotted child, assignedSlot answers: “Which <slot> am I assigned to?” That is useful for debugging layouts and building slot-aware components.

JavaScript
const slottedSpan = document.querySelector("my-paragraph span");
console.log(slottedSpan.assignedSlot); // HTMLSlotElement (or null)
💡
Beginner tip

Think of <slot> as a hole in the component’s shadow template, and slot="name" on a child as the key that fills that hole.

Understanding the Property

MDN: the assignedSlot property of the Element interface returns an HTMLSlotElement representing the <slot> the node is inserted in.

  • Read-only — you inspect assignment; you do not set this property.
  • Slottable child — works on light-DOM content assigned into a slot.
  • May be null — not slotted, or shadow root is closed.
  • Baseline — widely available since January 2020 (MDN).

📝 Syntax

JavaScript
assignedSlot

Value

An HTMLSlotElement instance, or null if the element is not assigned to a slot, or if the associated shadow root was attached with mode: "closed".

ItemDetail
TypeHTMLSlotElement | null
AccessRead-only
Assign viaslot="name" on light-DOM children
NeedsOpen shadow root to observe the slot from outside
⚠️
Closed shadow roots hide the slot

Even if content is visually slotted, assignedSlot returns null when the host’s shadow root uses mode: "closed". Prefer mode: "open" while learning and debugging.

📋 MDN Custom Element Example Shape

MDN’s simple-template pattern defines a custom element with a named slot my-text. Light-DOM content uses slot="my-text", then assignedSlot points at that <slot>:

JavaScript
<my-paragraph>
  <span slot="my-text">Let's have some different text!</span>
</my-paragraph>
JavaScript
class MyParagraph extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: "open" });
    shadow.innerHTML = '<slot name="my-text"></slot>';
  }
}
customElements.define("my-paragraph", MyParagraph);

const slottedSpan = document.querySelector("my-paragraph span");
console.log(slottedSpan.assignedSlot); // <slot name="my-text">
console.log(slottedSpan.assignedSlot.name); // "my-text"

Related learning: Shadow DOM, custom elements, and HTMLSlotElement.assignedNodes() (the view from the slot side).

⚡ Quick Reference

GoalCode / note
Read slotel.assignedSlot
Slot nameel.assignedSlot?.name
Assign in HTML<span slot="my-text">...</span>
Declare slot<slot name="my-text"></slot> in shadow tree
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.assignedSlot.

Kind
get only

Instance

Type
HTMLSlotElement?

Or null

Needs
open

Shadow mode

Baseline
widely

Jan 2020+

Examples Gallery

Examples follow MDN Element: assignedSlot. Labs use a tiny custom element with an open shadow root so you can inspect the property safely.

📚 Getting Started

Read the assigned slot and inspect its name (MDN shape).

Example 1 — Read assignedSlot

Log whether a slotted child has an assigned <slot>.

JavaScript
class MyParagraph extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: "open" });
    shadow.innerHTML = '<slot name="my-text"></slot>';
  }
}
customElements.define("my-paragraph", MyParagraph);

const slottedSpan = document.querySelector("my-paragraph span");
console.log(slottedSpan.assignedSlot !== null);
console.log(slottedSpan.assignedSlot.outerHTML);
Try It Yourself

How It Works

After the custom element upgrades with an open shadow root, the light-DOM <span slot="my-text"> is assigned to that named slot.

Example 2 — Read the Slot Name

MDN focuses on finding the slot; the name property is a clear beginner check.

JavaScript
const slottedSpan = document.querySelector("my-paragraph span");
const slot = slottedSpan.assignedSlot;
console.log(slot ? slot.name : null); // "my-text"
Try It Yourself

How It Works

assignedSlot returns the live HTMLSlotElement. From there you can read name, listen for slotchange, or call assignedElements().

📈 Null Cases & Snapshot

See when the property is null, and feature-detect support.

Example 3 — Not Assigned to a Slot

A normal element outside any custom host has no assigned slot.

JavaScript
const plain = document.createElement("span");
plain.textContent = "Not slotted";
document.body.appendChild(plain);
console.log(plain.assignedSlot); // null
Try It Yourself

How It Works

Always null-check before reading .name or calling methods on the result.

Example 4 — Closed Shadow Root Returns null

Same markup can still yield null when the shadow root is closed.

JavaScript
class ClosedCard extends HTMLElement {
  constructor() {
    super();
    const shadow = this.attachShadow({ mode: "closed" });
    shadow.innerHTML = '<slot name="title"></slot>';
  }
}
customElements.define("closed-card", ClosedCard);

const child = document.querySelector("closed-card span");
console.log(child.assignedSlot); // null (closed shadow)
Try It Yourself

How It Works

Closed mode is an encapsulation choice. Content may still render in the slot, but assignedSlot stays hidden from outside script.

Example 5 — Support Snapshot

Feature-detect and remember the open-shadow requirement.

JavaScript
console.log({
  supported: "assignedSlot" in Element.prototype,
  returns: "HTMLSlotElement or null",
  tip: "Use mode: \"open\" to inspect assignedSlot",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Use feature detection in older environments, then null-check every read.

🚀 Common Use Cases

  • Debugging which named slot a projected child landed in.
  • Building slot-aware helpers that style or measure slotted content.
  • Teaching Shadow DOM: compare light-DOM children with shadow <slot> nodes.
  • Verifying that a host uses an open shadow root during development.
  • Pairing with slotchange events when projected content updates.

🔧 How It Works

1

Host attaches a shadow root

Usually mode: "open" with one or more slots.

Shadow
2

Light-DOM child sets slot

slot="my-text" matches <slot name="my-text">.

Assign
3

assignedSlot points at the slot

From the child, you get the live HTMLSlotElement.

Read
4

You can inspect or react

Read name, listen for slotchange, or debug projection.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Read-only — assignment happens via the slot attribute / slotting rules.
  • Returns null for closed shadow roots even when content is projected.
  • Related: ariaValueText, attributes, EventTarget, JavaScript hub.

Browser Support

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

Baseline Widely available

Element.assignedSlot

Read-only — returns HTMLSlotElement or null for Shadow DOM slot assignment.

Baseline Widely available
Google Chrome Supported (Shadow DOM)
Yes
Microsoft Edge Supported (Shadow DOM)
Yes
Mozilla Firefox Supported (Shadow DOM)
Yes
Apple Safari Supported (Shadow DOM)
Yes
Opera Follow Chromium support
Yes
Internet Explorer No Shadow DOM / assignedSlot
No
assignedSlot Baseline

Bottom line: Use assignedSlot to discover whicha light-DOM child is assigned to. Prefer open shadow roots for inspection. Always null-check the result.

Conclusion

assignedSlot lets a slotted element report which <slot> it fills. Use open shadow roots while learning, null-check every read, and pair it with slot names and slotchange when building custom elements.

Continue with attributes, ariaValueText, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Match slot attribute names to <slot name>
  • Use mode: "open" while debugging
  • Null-check before reading .name
  • Prefer named slots for multi-region layouts
  • Listen for slotchange when content swaps

❌ Don’t

  • Try to assign el.assignedSlot = ... (read-only)
  • Assume a non-null result under closed shadow roots
  • Forget to define the custom element before querying
  • Confuse light-DOM children with nodes inside the shadow tree
  • Skip null checks in production helpers

Key Takeaways

Knowledge Unlocked

Five things to remember about assignedSlot

Read-only pointer from a light-DOM child to its Shadow DOM <slot>.

5
Core concepts
📝 02

HTMLSlotElement

or null

Type
🔍 03

Shadow slots

custom elements

Use
04

Baseline

widely available

Status
🎯 05

Open mode

needed to inspect

Tip

❓ Frequently Asked Questions

It returns the HTMLSlotElement the element is assigned to inside a Shadow DOM, or null if it is not assigned to a slot.
No. MDN marks Element.assignedSlot as Baseline Widely available (since January 2020). It is not Deprecated, Experimental, or Non-standard.
No. It is a read-only instance property. You assign content with the slot HTML attribute (or Slottable APIs), then read assignedSlot.
When the element is not assigned to any slot, or when the associated shadow root was attached with mode: "closed".
Put it as a child of the host and set slot="name", matching a <slot name="name"> inside the open shadow tree.
HTMLSlotElement.assignedNodes() and assignedElements() list the nodes assigned into a slot. assignedSlot asks from the child side.
Did you know?

assignedSlot looks from the child toward the slot. HTMLSlotElement.assignedNodes() looks the other way—from the slot toward its projected children.

Next: attributes

Learn the live NamedNodeMap of all attributes on an element.

attributes →

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