JavaScript Document append() Method

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

What You’ll Learn

document.append() is an instance method from the ParentNode API. It inserts nodes or strings after the last 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.append() and appendChild(), and five try-it labs.

01

Kind

Instance method

02

Inserts

After last child

03

Args

Nodes or strings

04

Returns

undefined

05

Live page

Use body

06

Status

Baseline

Introduction

Every web page has a document object—the root of the DOM tree. Most tutorials teach you to add content with document.body.append(...) or document.getElementById("app").append(...). Document.append() is different: it appends directly to the Document node itself, not inside <body>.

MDN: Document.append() inserts a set of Node objects or strings after the last child of the document. Strings are inserted as equivalent Text nodes. To append to an arbitrary element in the tree, see Element.append().

💡
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().

Related tutorials: Document constructor, Element.append(), Document.prepend(), documentElement.

Understanding document.append()

An instance method on every Document (ParentNode mixin).

  • Target — the Document node (not body or head).
  • Arguments — one or more Node objects or strings (MDN).
  • Strings — become Text nodes automatically.
  • Order — multiple arguments append in the order given.
  • Returnsundefined (unlike appendChild).
  • Empty document — typical first step: append root <html> or <svg> (MDN).
  • Live HTML page — appending a second <html> throws HierarchyRequestError (MDN).

📝 Syntax

General forms of Document.append (MDN):

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

Parameters

  • param1, …, paramN — a set of Node objects or strings to insert (MDN).

Return value

None (undefined).

Exceptions

  • HierarchyRequestError DOMException — 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.append(html);
// doc.children → HTMLCollection []

// On a live page — prefer body or a container
document.body.append("Hello");
document.getElementById("app").append(p, " ", span);

⚡ Quick Reference

GoalCode / note
Root on new Documentdoc.append(doc.createElement("html"))
Update live page contentdocument.body.append(...)
Append textel.append("Hello")
Append several itemsel.append(a, " ", b)
Return valueundefined
MDN statusBaseline Widely available (Apr 2018)

🔍 At a Glance

Four facts about document.append().

Call
document.append

Instance

Returns
undefined

MDN

Live page
use body

Not doc

Status
baseline

Apr 2018

📋 Document.append() vs Element.append()

document.append(...)element.append(...)
Receives onDocument rootAny Element
Main useNew Document() rootEveryday DOM updates
Live HTML pageSecond <html> failsWorks on body, div, etc.
Syntax & stringsSame ParentNode APISame ParentNode API

Examples Gallery

Examples follow MDN Document: append(). 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 — Append a root <html> to new Document()

MDN: when creating a new document without any existing element, append a root HTML element.

JavaScript
const doc = new Document();
const html = doc.createElement("html");
doc.append(html);

console.log(doc.children.length); // 1
console.log(doc.documentElement === html); // true
Try It Yourself

How It Works

An empty Document() has no documentElement until you append the root. See the Document constructor tutorial.

Example 2 — MDN: HierarchyRequestError on a live page

Appending a second <html> to an existing HTML document throws (MDN).

JavaScript
const html = document.createElement("html");

try {
  document.append(html);
} catch (err) {
  console.log(err.name); // "HierarchyRequestError"
}
Try It Yourself

How It Works

MDN: the operation would yield an incorrect node tree. Use document.body.append() instead for page content.

📈 Practical Patterns

What to use on a loaded page and how ParentNode helpers compare.

Example 3 — Use document.body.append() on a live page

Add content where beginners expect—inside <body>.

JavaScript
const p = document.createElement("p");
p.textContent = "Added to the page";

document.body.append(p);
// Same ParentNode API as document.append, different target
Try It Yourself

How It Works

See the full Element.append() tutorial for multi-argument and string patterns on elements.

Example 4 — Multiple nodes and strings

ParentNode append accepts several arguments in one call.

JavaScript
const doc = new Document();
const html = doc.createElement("html");
const body = doc.createElement("body");
const span = doc.createElement("span");

html.append(body);
body.append("Hello ", span, "!");

doc.append(html);
console.log(body.textContent); // "Hello !"
Try It Yourself

How It Works

Build structure on elements with append, then attach the root to the document with doc.append(html).

Example 5 — append() vs appendChild()

appendChild takes one node; append takes nodes and strings.

JavaScript
const doc = new Document();
const html = doc.createElement("html");

const returned = doc.appendChild(html);
console.log(returned === html); // true — appendChild returns the node

doc.append(doc.createComment("note")); // append returns undefined
console.log(doc.lastChild.nodeType); // 8 (Comment)
Try It Yourself

How It Works

Both work on Document nodes. Prefer append when you need strings or multiple items. See appendChild().

🚀 Common Use Cases

  • In-memory documents — set root <html> or <svg> on new Document() (MDN).
  • Test fixtures — build detached DOM trees before adopting into the live page.
  • Serialization pipelines — compose a document offline, then extract with XMLSerializer.
  • Live page UI — use document.body.append() or container el.append(), not document.append().
  • After parseHTML — parsed documents may already have structure; append only when the tree is empty.
  • Comments / text at doc level — rare; usually append inside elements instead.

🧠 How document.append() Inserts Nodes

1

Choose the parent

document.append targets the Document node itself (MDN).

Document
2

Pass nodes or strings

Strings become Text nodes; nodes append in order (MDN).

ParentNode
3

Validate hierarchy

Invalid trees throw HierarchyRequestError (MDN).

DOM rules
4

Children updated

New nodes appear after the document’s last child; method returns undefined.

📝 Notes

  • MDN: Baseline Widely available since April 2018.
  • Not Deprecated, Experimental, or Non-standard.
  • Same ParentNode specification as Element.append().
  • On a loaded HTML page, document.documentElement already exists—do not append another <html>.
  • For everyday updates, document.body.append() is the beginner-friendly choice.
  • Related: Document(), Element.append(), appendChild(), adoptNode().

Browser Support

Document.append() 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.append()

ParentNode append on Document — build roots on new Document(), use body.append on live pages.

Baseline Widely available
Google Chrome 54+
Yes
Microsoft Edge 17+
Yes
Mozilla Firefox 48+
Yes
Apple Safari 10+
Yes
Opera 41+
Yes
Internet Explorer No
No
append() 100% supported

Bottom line: Use document.append() to attach a root element to a new Document(). On a loaded page, prefer document.body.append() or Element.append().

Conclusion

document.append() inserts nodes or strings after the last 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.

Continue with ariaNotify(), ownerDocument, Element.append(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use doc.append(html) when bootstrapping new Document() (MDN)
  • Use document.body.append() for live page content
  • Pass strings directly—no createTextNode needed
  • Wrap risky appends in try/catch for HierarchyRequestError
  • Cross-link with Element.append() for element targets

❌ Don’t

  • Call document.append(html) on a loaded HTML page (MDN)
  • Assume append returns the inserted node
  • Confuse document.append with document.write
  • Append foreign-document nodes without adoptNode()
  • Forget DOM hierarchy rules for root elements

Key Takeaways

Knowledge Unlocked

Five things to remember about append()

Document-level ParentNode append—roots on new docs, body on live pages.

5
Core concepts
🗂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 after the last child of the Document. Strings become Text nodes (MDN).
No. MDN marks Document.append() 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.append() or document.documentElement.append() to add content. document.append() 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: appending a second html element to an existing HTML document throws HierarchyRequestError because the tree would be invalid.
append() accepts multiple nodes and strings and returns undefined. appendChild() accepts one Node and returns that child.
Did you know?

MDN notes that Document.append() and Element.append() share the same ParentNode specification—but on a normal web page you almost always call append on an element, not on document itself.

Next: ariaNotify()

Learn how document.ariaNotify() queues screen reader announcements at the document level.

ariaNotify() →

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.

6 people found this page helpful