Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Node.appendChild — appending a node

Node.appendChild adds a node as a parent's last child. It returns the supplied node and moves an existing node rather than copying it.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

parent.appendChild(node)

parent.appendChild(node) accepts one Node and returns that same object. This is a DOM API, not an ECMAScript function.

If node has a parent, it is moved, not copied. Appending an existing child moves it to the end. For a DocumentFragment, its children move in their existing order; the returned fragment becomes empty.

An invalid hierarchy, such as inserting an ancestor into its descendant, causes HierarchyRequestError. Unlike append(), this method accepts neither text nor multiple arguments as a node list.

The example requires a browser DOM; a Node.js version alone does not establish API support. The result BA demonstrates moving first.

Examples

const parent = document.createElement('div');
const first = document.createElement('span');
first.textContent = 'A';
const second = document.createElement('span');
second.textContent = 'B';

parent.appendChild(first);
parent.appendChild(second);
const returned = parent.appendChild(first);

console.log(parent.textContent);
console.log(returned === first);
console.log(parent.childNodes.length);

Result

BA
true
2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources