document.replaceChildren(...nodesOrStrings) replaces the existing children of a Document with a new set. With no arguments, it empties the document. Strings become Text nodes. Returns undefined.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.replaceChildren. Start with this line.
JavaScript
document.replaceChildren(…nodesOrStrings)
Call
What it does
document.replaceChildren(…nodesOrStrings)
Primary signature.
document.replaceChildren(...)
Call with the arguments your case needs.
Built into the browser. Call replaceChildren(...) on document — no import required.
Library declarations
JavaScript
document.replaceChildren(…nodesOrStrings)
Parameters
Parameter
Description
param1…paramN
Optional. New children; omit to empty
Return Value
Return Value
Description
*
Always undefined
JavaScript Examples
Example 1 — Empty a Document (MDN)
Call with no arguments to remove every Document child.
JavaScript
const doc = new Document();
const html = doc.createElement("html");
doc.append(html);
doc.replaceChildren();
console.log(doc.children.length); // 0
It replaces the existing children of a Document with a new set of Nodes or strings. With no arguments, the Document is emptied of all child nodes.
No. MDN marks Document.replaceChildren() as Baseline Widely available (across browsers since October 2020). It is not Deprecated, Experimental, or Non-standard.
Call document.replaceChildren() with no arguments. Then document.children is an empty HTMLCollection.
Zero or more Node objects or strings. Strings become Text nodes. Invalid trees throw HierarchyRequestError.
Usually no for everyday UI. Emptying or replacing the live document root is destructive. Prefer Element.replaceChildren() on body or a container, or use replaceChildren on a new Document() when building documents in memory.
Same ParentNode API. Document.replaceChildren() targets the Document node itself; Element.replaceChildren() targets an element such as a div or body — the common choice for UI updates.