JavaScript Element slot Property

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

What You’ll Learn

Element.slot is an instance property that reflects the HTML slot attribute. It returns the name of the shadow DOM slot the element is assigned to—or an empty string when no named slot is set. Learn reading, writing, and pairing it with <slot> in web components—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read / write

03

Type

String

04

Reflects

slot attribute

05

Empty

No named slot

06

Status

Baseline widely

Introduction

Web components often use slots as placeholders inside a shadow tree. Page authors fill those placeholders by placing light-DOM children into the host and giving them a slot attribute that matches a named <slot>.

element.slot is the JavaScript view of that attribute: it tells you which named slot the element targets, and you can change it at runtime.

JavaScript
const el = document.createElement("span");
el.slot = "my-text";
console.log(el.slot); // "my-text"
💡
Beginner tip

Think of slot as a mailing label. The shadow tree has named mailboxes (<slot name="...">). Light-DOM children use slot="..." to choose which mailbox they go into.

Understanding the Property

MDN: the slot property of the Element interface returns the name of the shadow DOM slot the element is inserted in. A slot is a placeholder inside a web component that users can fill with their own markup.

  • String value — the named slot, or "" when unset.
  • Reflects the attributeel.slot and slot="..." stay in sync.
  • Read and write — you can get or set the name from JavaScript.
  • Pairs with <slot> — useful for custom elements and templates.

📝 Syntax

JavaScript
element.slot
element.slot = "slot-name"

Value

A string: the name of the assigned shadow DOM slot, or an empty string when the element is not assigned to a named slot.

ItemDetail
Typestring
AccessRead / write
ReflectsHTML slot attribute
Named slote.g. "my-text"
Unset"" (empty string)
⚠️
Not the <slot> element

element.slot is a string name on slotted content. The placeholder itself is an HTMLSlotElement created with <slot> inside the shadow tree.

📋 Named Slot Pattern

MDN’s core idea: put a named <slot> in the shadow tree, then assign light-DOM children with a matching slot name. Reading element.slot returns that name.

JavaScript
// Light DOM child targeting a named slot
const span = document.createElement("span");
span.slot = "my-text";
span.textContent = "Let's have some different text!";

// Later:
console.log(span.slot); // "my-text"

Related learning: shadowRoot, part, and HTML <slot> / HTMLSlotElement.

⚡ Quick Reference

GoalCode / note
Read slot nameel.slot
Set slot nameel.slot = "title"
HTML equivalent<span slot="title">
Clear named slotel.slot = ""
Shadow placeholder<slot name="title">
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.slot.

Kind
get / set

Instance

Type
string

Slot name

Reflects
slot attr

HTML

Baseline
widely

Shadow DOM

Examples Gallery

Examples follow MDN Element: slot and show reading, writing, and using named slots with a simple host.

📚 Getting Started

Read the slot name from markup and from an unset element.

Example 1 — Read a Named Slot

An element with slot="my-text" reports that name in JavaScript.

JavaScript
const span = document.querySelector("[slot='my-text']");
console.log(span.slot); // "my-text"
Try It Yourself

How It Works

The property mirrors the HTML slot attribute value.

Example 2 — Empty String When Unset

Without a named slot, slot is an empty string.

JavaScript
const el = document.createElement("span");
console.log(el.slot);      // ""
console.log(el.slot === ""); // true
Try It Yourself

How It Works

Empty does not mean “broken”—it usually means the default (unnamed) slot path.

📈 Set & Component Use

Assign slots from JavaScript and connect them to a shadow host.

Example 3 — Set the Slot from JavaScript

Writing element.slot updates the attribute.

JavaScript
const el = document.createElement("span");
el.slot = "title";
console.log(el.getAttribute("slot")); // "title"
el.slot = "";
console.log(el.slot); // ""
Try It Yourself

How It Works

Use this when you build slotted content dynamically instead of hard-coding attributes.

Example 4 — Named Slot in a Simple Host

Match a light-DOM child to a named <slot> in an open shadow tree.

JavaScript
const host = document.createElement("div");
host.attachShadow({ mode: "open" });
host.shadowRoot.innerHTML = "";

const span = document.createElement("span");
span.slot = "my-text";
span.textContent = "Let's have some different text!";
host.appendChild(span);

console.log(span.slot); // "my-text"
Try It Yourself

How It Works

The shadow <slot name="my-text"> and the child’s slot property must match for content to project into that placeholder.

Example 5 — Support Snapshot

Feature-detect and remember the string / empty rules.

JavaScript
console.log({
  supported: "slot" in Element.prototype,
  returns: "string (slot name or \"\")",
  tip: "Reflects the HTML slot attribute",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Safe to use widely. It is the main JavaScript handle for named slot assignment on elements.

🚀 Common Use Cases

  • Checking which named slot a light-DOM child targets.
  • Assigning slot names when building component content in JavaScript.
  • Moving content between named slots by changing element.slot.
  • Debugging why projected content does not appear in a <slot>.
  • Pairing with shadowRoot and part in custom elements.

🔧 How It Works

1

Shadow tree defines placeholders

<slot name="..."> creates named insertion points.

Shadow
2

Light DOM children set a label

Via slot="..." or element.slot = "...".

Assign
3

Browser projects matching content

Names must match for named slots to fill.

Project
4

element.slot reports the name

Read or rewrite the assignment anytime.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Returns a string—never a ShadowRoot or HTMLSlotElement.
  • Empty string means no named slot assignment (default-slot path).
  • Related: shadowRoot, tagName, part, JavaScript hub.

Browser Support

Element.slot is Baseline Widely available (MDN). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.slot

Read / write — returns the named shadow DOM slot string (or empty).

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Not supported
No
slot Baseline

Bottom line: Use element.slot to read or set which named shadow DOM slot a light-DOM child targets.

Conclusion

slot is the string property that connects light-DOM content to named <slot> placeholders in a shadow tree. Read it to inspect assignment; write it to move content between slots.

Continue with tagName, part, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Match el.slot names to <slot name> exactly
  • Use clear slot names like title, body, actions
  • Prefer setting el.slot when building DOM in JS
  • Document your component’s public slot API
  • Check for "" when debugging default-slot content

❌ Don’t

  • Confuse element.slot with the <slot> element itself
  • Expect a non-empty string for default (unnamed) slots
  • Use mismatched names between attribute and <slot name>
  • Treat slot as a DOM node reference
  • Skip documenting which slots your custom element supports

Key Takeaways

Knowledge Unlocked

Five things to remember about slot

Read/write string that names which shadow DOM slot an element targets.

5
Core concepts
📝02

string

slot name

Type
🔍03

"" when unset

default path

Empty
04

Baseline

widely available

Status
🎯05

Match names

to <slot>

Tip

❓ Frequently Asked Questions

It returns the name of the shadow DOM slot the element is assigned to. It reflects the HTML slot attribute as a string.
No. MDN marks Element.slot as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
An empty string means the element is not assigned to a named slot (it may still fall into the default unnamed slot).
No. You can read and write element.slot. Setting it updates the matching slot attribute.
Inside a shadow tree you place <slot name="title">. Light-DOM children with slot="title" fill that named slot. element.slot then returns "title".
No. Element.slot is a string property on slotted content. <slot> elements are HTMLSlotElement instances inside the shadow tree.
Did you know?

MDN’s classic demo uses a custom element with a shadow slot named my-text. Reading document.querySelector("my-paragraph span").slot logs "my-text"—the same idea as Example 1 and Example 4 on this page.

Previous: shadowRoot

Learn how Element.shadowRoot accesses open Shadow DOM trees.

← shadowRoot

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