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
Fundamentals
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!
A set of Node objects or strings to insert before the first child (MDN). Strings are inserted as equivalent Text nodes.
Return value
Type
Description
undefined
None (undefined) — the method updates the DOM in place (MDN).
Exceptions
HierarchyRequestErrorDOMException — 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
Cheat Sheet
⚡ Quick Reference
Goal
Code
Prepend one element
el.prepend(child)
Prepend text
el.prepend("Headline: ")
Prepend several items
el.prepend(a, " ", b)
Classic single-node API
el.insertBefore(node, el.firstChild)
Return value
undefined
MDN status
Baseline Widely available (since April 2018)
Snapshot
🔍 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
Compare
📋 prepend() vs append() vs insertBefore()
el.prepend(...)
el.append(...)
parent.insertBefore(node, ref)
Position
Before first child
After last child
Before reference node
Accepts strings?
Yes (as Text)
Yes (as Text)
No — Node only
Multiple args?
Yes
Yes
One node per call
Return value
undefined
undefined
Inserted node
Best for
Start of child list
End of child list
Precise 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.
Hands-On
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> ]
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> ]
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)
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.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerNot 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.
Wrap Up
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.
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.prepend()
Add first children—nodes or text, one call.
5
Core concepts
📝01
Returns
undefined
API
📄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.