JavaScript Node appendChild() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is appendChild()?

parent.appendChild(child) adds a node to the end of the parent’s child list (MDN). If child already exists in the document, it is moved — not copied.

Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Prefer Element.append() when you need multiple args or text strings.

Remember
parent.appendChild(child)
→ child becomes the last child of parent
→ returns child (not parent)

If child already in the document → moved here
Need a copy? → cloneNode() first

Chaining nests:
  parent.appendChild(a).appendChild(b)
  → b is inside a (returns the child each time)
GoalPrefer
One node at the endappendChild(child)
Many nodes / stringsElement.append(...)
Insert before a siblinginsertBefore()

How to Use appendChild()

Create (or select) a node, then append it to a parent.

  1. Get a parent node (often an element).
  2. Create or find the child (createElement, existing node, …).
  3. Call parent.appendChild(child).
  4. Use the return value if you need the child reference.
PieceRequired?Role
parentYesNode that receives the child
childYesNode to append (or a DocumentFragment)
Return—The appended child (empty fragment if fragment)

Pseudocode

Pseudocode
child = createElement("p")   // or an existing node
parent.appendChild(child)
→ child is last among parent.childNodes

if child already has a parent:
  remove from old place, then insert at end of parent

copy needed?
  parent.appendChild(child.cloneNode(true))

Cheat Sheet

Keep these facts handy when building DOM trees.

TopicDetail
Syntaxparent.appendChild(child)
ReturnsAppended child (or empty fragment)
Existing nodeMoved, not cloned
ChainingNests — returns child each time
vs append()Single Node arg vs multi + strings
ErrorsHierarchyRequestError if tree rules break
StandardBaseline Widely available (since July 2015)

Live Preview

Append a new item, move an existing item, or append a clone into the list.

List
  • Existing item

Browser demo of Node.appendChild() — move vs clone.

Result
Choose an action, then Run…

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Append a New Element

Create a paragraph and append it to the body (MDN-style).

JavaScript
const p = document.createElement("p");
p.textContent = "Hello";
document.getElementById("box").appendChild(p);
Try It Yourself

How It Works

  1. Create the element. createElement("p") builds a node.
  2. Set text. Fill content before or after append.
  3. Append to parent. Becomes the last child of #box.
  4. Visible in the DOM. The page updates immediately.

Example 2 — Use the Return Value

MDN: appendChild returns the child — keep the reference.

JavaScript
const p = document.body.appendChild(document.createElement("p"));
p.textContent = "kept the reference";
console.log(p.parentNode === document.body); // true
Try It Yourself

How It Works

  1. Append in one expression. Create + append together.
  2. Capture the return. Same node you appended.
  3. Edit after append. Set textContent on that reference.
  4. Not the parent. Do not expect a fluent “parent chain.”

Example 3 — Moving an Existing Node

Appending a node that is already in the tree moves it (MDN).

JavaScript
const list = document.getElementById("list");
const first = list.firstElementChild;

list.appendChild(first);
// first was removed from the start and added at the end
Try It Yourself

How It Works

  1. Pick an existing child. Already in the document.
  2. Append again. DOM moves it — one place only.
  3. No clone. Same node object, new position.
  4. Need two copies? Use cloneNode first.

Example 4 — Chaining Nests (Pitfall)

MDN warning: chained appendChild nests, it does not add siblings.

JavaScript
const box = document.getElementById("box");

// Nested: p2 inside p1 — NOT three siblings
box
  .appendChild(document.createElement("p"))
  .appendChild(document.createElement("p"));

console.log(box.children.length); // 1
Try It Yourself

How It Works

  1. First append returns p1. Not box.
  2. Second append is on p1. So p2 becomes a child of p1.
  3. Siblings need separate calls on the same parent.
  4. Or use Element.append with multiple arguments.

Example 5 — Append a Clone

Copy with cloneNode, then append — original stays put.

JavaScript
const list = document.getElementById("list");
const item = document.getElementById("item");

list.appendChild(item.cloneNode(true));
console.log(list.children.length);
Try It Yourself

How It Works

  1. Clone first. cloneNode(true) deep-copies.
  2. Append the copy. Original stays in place.
  3. Count children. Length increases by one.
  4. Clones are not live-synced with the original (MDN).

Edge Cases & Pitfalls

Check these before building complex trees with appendChild.

move

Existing nodes are moved

MDN: a node cannot be in two places. Append moves it unless you cloneNode.

return

Returns the child, not the parent

Chaining nests elements. Call appendChild separately on the parent for siblings.

hierarchy

HierarchyRequestError

Thrown for illegal trees (e.g. appending a node into its own descendant).

append

Element.append() for multi-args

MDN: prefer append() when you need multiple nodes or strings on an Element.

Browser Support

Node.appendChild() is marked Baseline Widely available on MDN (since July 2015).

✓ Baseline · Widely available

Node.appendChild()

Adds a child to the end of a parent. Moves existing nodes. Returns the appended child. Prefer Element.append() for multiple arguments or strings.

100% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
appendChild() Excellent

Bottom line: Safe everyday DOM API. Remember: move vs clone, and chaining nests. See the MDN appendChild reference.

Performance Notes

ApproachCostBest for
One appendChildOne layout touchSingle node
Many appends in a loopCan reflow oftenAvoid for large lists
DocumentFragment then one appendOne insertBatch many children

For many nodes, build into a fragment (or use append with multiple args), then attach once — fewer layout thrashers than N separate appends.

Key Takeaways

  • Adds last: child becomes the end of parent’s children.
  • Moves: existing document nodes relocate, not copy.
  • Returns child: chaining nests — call on the parent for siblings.
  • Prefer append() for multiple nodes/strings on an Element.

One line: appendChild puts a node last under a parent — moving it if it already lived elsewhere.

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 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.

Next: cloneNode()

Copy a node when you need two instances in the tree.

cloneNode tutorial →

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.

8 people found this page helpful