The Node.appendChild() method adds a node to the end of a parent’s children. Learn how it moves existing nodes, what it returns, how DocumentFragment works, and how it compares to Element.append().
01
Kind
Instance method
02
Adds
Last child
03
Existing node
Moves (no clone)
04
Returns
The child node
05
vs append()
One node; Node API
06
Status
Baseline widely
Fundamentals
Introduction
Building UI in the DOM usually means creating elements and attaching them to parents. appendChild() is the classic way to put one node at the end of another node’s children.
A critical beginner surprise: if the child is already in the document, appendChild()moves it. A node cannot live in two places at once. Use cloneNode() when you need a copy.
💡
Beginner tip
Remember the return value: appendChild gives you the child, not the parent. Chaining calls nests elements instead of adding siblings.
Concept
Understanding appendChild()
MDN: appendChild() adds a node to the end of the list of children of a specified parent node.
New node — created with createElement / createTextNode, then appended.
Existing node — removed from its old parent and attached to the new one.
DocumentFragment — its children move into the parent; the fragment is emptied.
Invalid trees — may throw HierarchyRequestError (cycles, wrong node types, etc.).
Foundation
📝 Syntax
JavaScript
parent.appendChild(child);
Parameters
child — The node to append (commonly an Element or Text).
Return value
The appended child node. For a DocumentFragment, the emptied fragment is returned after its contents move.
Compare
⚖️ appendChild() vs Element.append()
appendChild()
Element.append()
Available on
Any Node parent
Element only
Arguments
One node
Multiple nodes and/or strings
Return value
The appended child
undefined
Strings
Not accepted (use a text node)
Accepted as text
Best for
Classic single-node attach / move
Batch appends on elements
MDN notes you can prefer Element.append() when your parent is an element and you want multiple arguments or string support. Both are Baseline and valid in modern apps.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Create and append
parent.appendChild(document.createElement("p"));
Keep a reference
const p = parent.appendChild(document.createElement("p"));
Move an existing node
newParent.appendChild(existingNode);
Append a copy
parent.appendChild(node.cloneNode(true));
Build offline, then attach
parent.appendChild(fragment);
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Node.appendChild().
Returns
Node (child)
Not the parent
Baseline
widely
Since July 2015
Existing node
moves
Does not clone
Position
last child
End of children
Hands-On
Examples Gallery
Examples follow MDN Node.appendChild() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Create a node, append it, and see how existing nodes move.
Example 1 — Create and Append a Paragraph
MDN-style: create an element, then attach it to body (or any parent).
JavaScript
const p = document.createElement("p");
p.textContent = "Hello from appendChild";
document.getElementById("host").appendChild(p);
console.log(document.getElementById("host").lastChild.textContent);
// "Hello from appendChild"
After the call, item is no longer under boxA. The same node object now lives under boxB.
📈 Return Value, Fragments & Copies
Capture the child, build trees with fragments, and clone when you need duplicates.
Example 3 — Return Value and Nesting Gotcha
Use the return value to keep a reference—but do not chain for siblings.
JavaScript
const host = document.getElementById("host");
// Good: keep a reference to the new paragraph
const p = host.appendChild(document.createElement("p"));
p.textContent = "First";
// Bad for siblings: each call returns the child, so this nests
host
.appendChild(document.createElement("p"))
.appendChild(document.createElement("p"));
console.log(host.children.length); // 2 (First + outer p)
console.log(host.lastChild.children.length); // 1 (nested p)
Nesting via the return value is intentional here: section → ul → li. Appending the fragment moves its contents into host and leaves the fragment empty.
Example 5 — Clone Before Append to Keep Both
Need the original and a copy? Clone first, then append the clone.
Node.appendChild() is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Node.appendChild()
Safe for production. Prefer Element.append() when you need multiple nodes or strings on an Element.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
appendChild()Excellent
Bottom line: Use appendChild for a single Node attach/move; use cloneNode for copies; watch the return value when chaining.
Wrap Up
Conclusion
Node.appendChild() attaches one node as the last child of a parent. It moves existing nodes, returns the child (so chaining nests), and works well with DocumentFragment and cloneNode.
Ignore Element.append() when you need strings / multiple args
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about appendChild()
Attach one child at the end of a parent’s children.
5
Core concepts
➕01
Last child
end of children
API
🔁02
Moves nodes
does not clone
Gotcha
↪️03
Returns child
chaining nests
Return
📁04
Fragment
contents move
Batch
📋05
cloneNode
for copies
Copy
❓ Frequently Asked Questions
It adds the given child node to the end of the parent’s list of children. If that child already exists elsewhere in the document, appendChild moves it — it does not clone it.
No. MDN marks Node.appendChild() as Baseline Widely available (since July 2015). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
It returns the appended child node. If you pass a DocumentFragment, it returns that fragment after its children have been moved (so the fragment is empty).
Because each call returns the child you just appended, not the parent. body.appendChild(p1).appendChild(p2) puts p2 inside p1, not as a sibling of p1.
Element.append() can take multiple nodes and text strings in one call. Prefer it when you are working with an Element and need those features. appendChild still works on any Node parent and remains the classic single-node API.
Call node.cloneNode(true) (or false for a shallow copy), then appendChild the clone. Appending the original always moves it.
Did you know?
MDN shows you can write const paragraph = document.body.appendChild(document.createElement("p")); so you still have a reference to the new element—because appendChild returns the child, not the parent.