JavaScript Element prepend() Method

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

What You’ll Learn

Element.prepend() is an instance method from the ParentNode API. It inserts nodes or strings before the first child of the element. Learn multi-argument prepends, text coercion, how it compares to append() and insertBefore(), and five try-it labs.

01

Kind

Instance method

02

Inserts

Before first child

03

Args

Nodes or strings

04

Returns

undefined

05

vs append

First vs last

06

Status

Baseline widely

Introduction

insertBefore() adds one node at a time. prepend() is the modern ParentNode helper: add several nodes and plain strings in a single call, all as the new first children of the element.

Strings become Text nodes automatically—no createTextNode required for simple labels.

💡
Beginner tip

prepend() adds children inside the element at the start. before() adds siblings before the element. Different place in the tree!

This page is part of JavaScript Element. Related topics include append() and before().

Understanding the prepend() Method

Calling el.prepend(...nodes) inserts each argument at the start of el’s child list, in the order you pass them.

  • It is an instance method on Element (ParentNode mixin).
  • Arguments can be Node objects or strings (as Text).
  • Multiple arguments prepend in the order given (MDN).
  • Existing nodes in the document are moved, not cloned.
  • Return value is undefined (unlike insertBefore).
  • MDN: prepend is unscopable—unavailable inside a with statement.

📝 Syntax

General forms of Element.prepend (MDN):

JavaScript
prepend(param1)
prepend(param1, param2)
prepend(param1, param2, /* …, */ paramN)

Parameters

NameTypeDescription
param1, …, paramNNode or StringA set of Node objects or strings to insert before the first child (MDN). Strings are inserted as equivalent Text nodes.

Return value

TypeDescription
undefinedNone (undefined) — the method updates the DOM in place (MDN).

Exceptions

  • HierarchyRequestError DOMException — thrown when the node cannot be inserted at the specified point in the hierarchy (MDN).

Common patterns

JavaScript
let div = document.createElement("div");
let p = document.createElement("p");
let span = document.createElement("span");

div.append(p);
div.prepend(span);             // span before p (MDN)

div.prepend("Headline: ");     // text before children
div.prepend("Hi ", p, "!");    // text + element + text

⚡ Quick Reference

GoalCode
Prepend one elementel.prepend(child)
Prepend textel.prepend("Headline: ")
Prepend several itemsel.prepend(a, " ", b)
Classic single-node APIel.insertBefore(node, el.firstChild)
Return valueundefined
MDN statusBaseline Widely available (since April 2018)

🔍 At a Glance

Four facts to remember about Element.prepend().

Returns
undefined

Updates the DOM in place

Baseline
widely

Since April 2018

Where
first children

Start of child list

Strings
→ Text

Auto Text nodes

📋 prepend() vs append() vs insertBefore()

el.prepend(...)el.append(...)parent.insertBefore(node, ref)
PositionBefore first childAfter last childBefore reference node
Accepts strings?Yes (as Text)Yes (as Text)No — Node only
Multiple args?YesYesOne node per call
Return valueundefinedundefinedInserted node
Best forStart of child listEnd of child listPrecise reference position

MDN: prepend() and append() share the same ParentNode design—strings, multiple arguments, and undefined return—but insert at opposite ends of the child list.

Examples Gallery

Examples follow MDN Element.prepend() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Prepend an element, text, or both—matching MDN’s core demos.

Example 1 — Prepending an Element (MDN)

Append a <p>, then prepend a <span> before it.

JavaScript
let div = document.createElement("div");
let p = document.createElement("p");
let span = document.createElement("span");
div.append(p);
div.prepend(span);

console.log(div.childNodes); // NodeList [ <span>, <p> ]
Try It Yourself

How It Works

MDN: span becomes the first child; p stays as the second child. Order in childNodes is [span, p].

Example 2 — Prepending Text (MDN)

Add a headline prefix before existing text content.

JavaScript
let div = document.createElement("div");
div.append("Some text");
div.prepend("Headline: ");

console.log(div.textContent); // "Headline: Some text"
Try It Yourself

How It Works

MDN: prepend("Headline: ") inserts a Text node before the existing “Some text” child.

📈 Practical Patterns

Mix text and elements, prepend several children, and move existing nodes.

Example 3 — Element and Text Together (MDN)

Pass a string and an element in one call—order is preserved at the start.

JavaScript
let div = document.createElement("div");
let p = document.createElement("p");
div.prepend("Some text", p);

console.log(div.childNodes);
// NodeList [ #text "Some text", <p> ]
Try It Yourself

How It Works

First the Text node “Some text”, then the empty <p>—both at the beginning of div (MDN).

Example 4 — Several Children in One Call

Prepend a label trail with mixed nodes at the start of a list item.

JavaScript
const item = document.getElementById("item");
const badge = document.createElement("strong");
badge.textContent = "NEW";
const note = document.createElement("em");
note.textContent = "sale";

item.prepend(badge, " — ", note, " ");

console.log(item.innerHTML);
// <strong>NEW</strong> — <em>sale</em> Shoes
Try It Yourself

How It Works

One call prepends four nodes at the start: strong, text, em, text—before any existing content in item.

Example 5 — Moving an Existing Node to the Front

If the node is already in the document, prepend() relocates it to the start.

JavaScript
const list = document.getElementById("list");
const a = document.getElementById("a");
const b = document.getElementById("b");

list.prepend(a);

console.log([...list.children].map((el) => el.id).join(", "));
// "a, b"  (a moved to the front)
Try It Yourself

How It Works

a leaves its old position and becomes the first child. Use cloneNode(true) if you need a duplicate instead.

🚀 Common Use Cases

  • Adding headlines or labels before existing content (MDN text example).
  • Inserting badges or icons at the start of list items.
  • Replacing several insertBefore calls with a single prepend.
  • Moving an existing child to the front of a container.
  • Teaching ParentNode helpers vs classic Node methods.
  • Pairing with append() for clear start vs end placement.

🧠 How prepend() Adds First Children

1

Call on the parent element

You invoke el.prepend(...) on the container that should gain children at the start.

Parent
2

Normalize arguments

Strings become Text nodes; existing nodes may detach from their old place.

Prepare
3

Insert at the start

New nodes become the first children, in argument order (MDN).

Prepend
4

DOM updated; return undefined

Check childNodes or innerHTML—there is no return payload.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available since April 2018).
  • prepend() inserts children at the start—not siblings.
  • Return value is always undefined.
  • Invalid hierarchy can raise HierarchyRequestError.
  • MDN: prepend is unscopable (not available inside with).
  • Related: append(), before(), moveBefore(), JavaScript hub.

Browser Support

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

Baseline Widely available

Element.prepend()

Safe for production. Prefer prepend() when you need strings or multiple children at the start.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Not supported · Use insertBefore
No
prepend() Excellent

Bottom line: Use prepend() for modern multi-node and text inserts at the start. Use insertBefore() when you need a precise reference node or work with non-Element Node parents.

Conclusion

Element.prepend() is the modern way to add first children—nodes, text, or several at once. It returns undefined and updates the element’s child list at the start.

Continue with append(), before(), pseudo(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use prepend() for multiple nodes + strings at the start
  • Pass strings when you need Text nodes quickly (MDN)
  • Expect moves when reusing an existing node
  • Pair with append() for opposite-end inserts
  • Verify with childNodes or textContent

❌ Don’t

  • Expect a returned node (it is undefined)
  • Confuse prepend (children) with before (siblings)
  • Assume IE supports prepend()
  • Forget that invalid trees can throw HierarchyRequestError
  • Use with (el) { prepend(...) }—it is unscopable (MDN)

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.prepend()

Add first children—nodes or text, one call.

5
Core concepts
📄 02

Where

start of children

Parent
✍️ 03

Strings

become Text

Coerce
04

Baseline

widely available

Status
05

vs append

first vs last

Compare

❓ Frequently Asked Questions

It inserts one or more Node objects or strings before the first child of the element (MDN). Strings are inserted as equivalent Text nodes.
No. MDN marks Element.prepend() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
prepend() inserts before the first child; append() inserts after the last child. Both accept multiple nodes and strings and return undefined.
Nothing useful — the return value is undefined (MDN). Inspect childNodes or textContent to verify what was added.
Yes. Pass a string: el.prepend("Headline: "). The browser creates a Text node for you (MDN).
Like appendChild and append, prepend moves the node to the new position. Use cloneNode() if you need a copy.
Did you know?

MDN marks prepend() as unscopable: inside a with (div) { … } block, calling prepend("foo") throws ReferenceError. Prefer normal method calls—div.prepend("foo")—and avoid with entirely in modern code.

More Element Topics

Browse Element methods and properties to keep building DOM skills.

pseudo() →

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.

8 people found this page helpful