document.prepend() inserts one or more Node objects or strings before the first child of the Document. Strings become Text nodes. On a normal loaded page, you usually want document.body.prepend() instead.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.prepend. Start with this line.
JavaScript
document.prepend(…nodesOrStrings)
Call
What it does
document.prepend(…nodesOrStrings)
Primary signature.
document.prepend(...)
Call with the arguments your case needs.
Built into the browser. Call prepend(...) on document — no import required.
Library declarations
JavaScript
document.prepend(…nodesOrStrings)
Parameters
Parameter
Description
param1…paramN
Required. Nodes and/or strings to insert
Return Value
Return Value
Description
*
Always undefined
JavaScript Examples
Example 1 — Prepend a Root to new Document()
MDN pattern: attach an <html> root to an empty document.
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
Create a paragraph. This is normal page content, not a document root.
Prepend to body. Pass a node and a string in one call.
Order matters. Arguments keep order: note is first, then the text node.
Why this path. On live pages, Element.prepend / body.prepend is the safe ParentNode prepend.
Compatibility
Browser Support
Document.prepend() is a browser DOM API marked Baseline Widely available on MDN (across browsers since April 2018).
✓ 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 Firefox49+
Yes
Apple Safari10+
Yes
Opera41+
Yes
Internet ExplorerNo
No
prepend()Excellent in browsers
Bottom line: Safe in modern browsers. On a loaded page prefer document.body.prepend(). See the MDN Document.prepend() reference.
Frequently Asked Questions
It inserts one or more Node objects or strings before the first child of the Document. Strings become Text nodes. The return value is undefined.
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, use document.body.prepend() (or Element.prepend) to add content. document.prepend() is mainly for attaching a root html or svg element to a new empty Document().
undefined. Check document.children or documentElement to verify what was added.
A live HTML document already has a root html element. Prepending another one throws HierarchyRequestError because the tree would be invalid.
Both are ParentNode methods and accept multiple nodes/strings. prepend() inserts before the first child; append() inserts after the last child. Both return undefined.