Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Node.removeChild(): detaching a child node

The parent.removeChild(child) method detaches a direct child from its parent and returns the same node, which can be inserted again.

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.removeChild(child)

Node.removeChild is an instance method of the Node interface, defined by the DOM standard, not ECMAScript. Call parent.removeChild(child) on the parent; child can also be a text node.

The method returns the same child object. After detachment, its parentNode is null; the retained node can be reinserted using appendChild. Detachment does not destroy the object or its subtree.

If child is a Node but is not a direct child of parent, the method throws a DOMException named NotFoundError. This includes passing a deeper descendant or trying to remove an already detached child again. Passing null throws a TypeError during Web IDL argument conversion, before the parent check. The example requires a browser DOM; a Node.js version alone does not establish API availability.

Examples

const parent = document.createElement('div');
const child = document.createTextNode('Hello');
parent.appendChild(child);

const removed = parent.removeChild(child);
console.log(removed === child);
console.log(child.parentNode === null);
console.log(parent.childNodes.length);

parent.appendChild(removed);
console.log(parent.textContent);

Result

true
true
0
Hello

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