document.prepend() is an instance method from the ParentNode API. It inserts nodes or strings before the first child of the Document. Learn when to use it on a fresh new Document(), why it throws on a loaded page, how it compares to Element.prepend() and Document.append(), and five try-it labs.
01
Kind
Instance method
02
Inserts
Before first child
03
Args
Nodes or strings
04
Returns
undefined
05
Live page
Use body
06
Status
Baseline
Fundamentals
Introduction
Every web page has a document object—the root of the DOM tree. Most tutorials teach you to add content with document.body.prepend(...) or document.getElementById("app").prepend(...). Document.prepend() is different: it prepends directly to the Document node itself, not inside <body>.
MDN: Document.prepend() inserts a set of Node objects or strings before the first child of the document. Strings are inserted as equivalent Text nodes. To prepend to an arbitrary element in the tree, see Element.prepend().
💡
Beginner tip
On a normal loaded page, the document already has an <html> root. You usually update document.body or a container element—not document itself. MDN’s main use case is building a brand-new in-memory document with new Document().
param1, …, paramN — a set of Node objects or strings to insert (MDN).
Return value
None (undefined).
Exceptions
HierarchyRequestErrorDOMException — thrown when the node cannot be inserted at the specified point in the hierarchy (MDN).
Common patterns
JavaScript
// Build a new in-memory document (MDN)
const doc = new Document();
const html = doc.createElement("html");
doc.prepend(html);
// doc.children → HTMLCollection []
// On a live page — prefer body or a container
document.body.prepend("Hello");
document.getElementById("app").prepend(p, " ", span);
Compare
⚖️ Document.prepend vs Document.append vs Element.prepend
API
Inserts
Strings OK?
Typical use
document.prepend(...)
Before first Document child
Yes
Root <html> on new Document()
document.append(...)
After last Document child
Yes
Same root-building case
document.body.prepend(...)
Start of <body>
Yes
Add UI at top of a live page
el.prepend(...)
Start of any element
Yes
General DOM updates
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Root on new Document
doc.prepend(doc.createElement("html"))
Update live page content
document.body.prepend(...)
Prepend text
el.prepend("Hello")
Prepend several items
el.prepend(a, " ", b)
Return value
undefined
MDN status
Baseline Widely available (Apr 2018)
Snapshot
🔍 At a Glance
Four facts about document.prepend().
Call
document.prepend
Instance
Returns
undefined
MDN
Live page
use body
Not doc
Status
baseline
Apr 2018
Compare
📋 Document.prepend() vs Element.prepend()
document.prepend(...)
element.prepend(...)
Receives on
Document root
Any Element
Main use
New Document() root
Everyday DOM updates
Live HTML page
Second <html> fails
Works on body, div, etc.
Syntax & strings
Same ParentNode API
Same ParentNode API
Hands-On
Examples Gallery
Examples follow MDN Document: prepend(). Start with a fresh Document(), then see what happens on a live page.
📚 Getting Started
MDN’s core examples: build a document root and avoid invalid trees.
Example 1 — Prepend a root <html> to new Document()
MDN: when creating a new document without any existing element, prepend a root HTML element.
JavaScript
const doc = new Document();
const html = doc.createElement("html");
doc.prepend(html);
console.log(doc.children.length); // 1
console.log(doc.documentElement === html); // true
MDN: the operation would yield an incorrect node tree. Use document.body.prepend() instead for page content.
📈 Practical Patterns
What to use on a loaded page and how ParentNode helpers compare.
Example 3 — Use document.body.prepend() on a live page
Add content where beginners expect—inside <body>.
JavaScript
const p = document.createElement("p");
p.textContent = "Added to the top of the page";
document.body.prepend(p);
// Same ParentNode API as document.prepend, different target
Document.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
Document.prepend()
ParentNode prepend on Document — build roots on new Document(), use body.prepend on live pages.
BaselineWidely available
Google Chrome54+
Yes
Microsoft Edge17+
Yes
Mozilla Firefox48+
Yes
Apple Safari10+
Yes
Opera41+
Yes
Internet ExplorerNo
No
prepend()100% supported
Bottom line: Use document.prepend() to attach a root element to a new Document(). On a loaded page, prefer document.body.prepend() or Element.prepend().
Wrap Up
Conclusion
document.prepend() inserts nodes or strings before the first child of the Document. MDN’s key lesson: use it to attach a root <html> or <svg> to a new empty document; on a live page, update document.body or specific elements instead.
Call document.prepend(html) on a loaded HTML page (MDN)
Assume prepend returns the inserted node
Confuse document.prepend with document.write
Prepend foreign-document nodes without adoptNode()
Forget DOM hierarchy rules for root elements
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about prepend()
Document-level ParentNode prepend—roots on new docs, body on live pages.
5
Core concepts
📝01
Kind
instance
document.
🗂02
Target
Document
MDN
🔗03
Live page
use body
Tip
✓04
Returns
undefined
ParentNode
🛡05
Status
baseline
2018
❓ Frequently Asked Questions
It inserts one or more Node objects or strings before the first child of the Document. Strings become Text nodes (MDN).
No. MDN marks Document.prepend() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
On a loaded HTML page, the document already has a root html element, so use document.body.prepend() or document.documentElement.prepend() to add content. document.prepend() is mainly for building a new empty Document() with a root html or svg element (MDN).
Nothing useful — the return value is undefined. Check document.children or childNodes to verify what was added.
MDN: prepending a second html element to an existing HTML document throws HierarchyRequestError because the tree would be invalid.
Both are ParentNode methods. prepend() inserts before the first child; append() inserts after the last child. Both accept multiple nodes/strings and return undefined (MDN).
Did you know?
MDN notes that Document.prepend() and Element.prepend() share the same ParentNode specification—but on a normal web page you almost always call prepend on an element, not on document itself.