CKTIS / JavaScript
Document.documentElement — the document's root element
Document.documentElement returns the document's root element or null. The property is read-only, but the element itself can be modified.
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
document.documentElementRead Document.documentElement on a Document instance, for example as document.documentElement. It returns the Element whose parent is the document, or null when none exists. A document can have at most one such element.
The HTML parser normally creates an html element as the document root, but the DOM does not require that tag name. If you replace the root with another element, such as body, the property returns the replacement. In XML its name depends on the document. Do not confuse this property with firstChild, which may instead reference a document type declaration.
Read-only does not mean the returned element is immutable. The example removes a separate XML document's root; the next read returns null. This is a DOM API, not an ECMAScript language feature.
Examples
const doc = document.implementation.createDocument(null, "root");
console.log(doc.documentElement.localName);
doc.documentElement.remove();
console.log(doc.documentElement);Result
root null
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.