The Document() constructor creates a new in-memoryDocument. Learn how it differs from window.document, how to build detached trees with createElement and appendChild(), when to prefer createHTMLDocument(), and five hands-on examples.
01
Create
new Document()
02
Returns
Document
03
Parameters
none
04
Detached
not the live page
05
Alt
createHTMLDocument
06
Status
Baseline widely
Fundamentals
Introduction
Every web page has a document object—the live DOM tree the browser parsed from HTML. Sometimes you need a second document in memory: for tests, templates, or building markup before inserting it into the page.
new Document() gives you that blank slate. Nodes you create inside it belong to the new document until you move or copy them elsewhere.
new Document() does not change what visitors see. It is a sandbox document. To show content, build nodes and attach them to the live page (or serialize the tree as HTML).
Concept
Understanding the Document() Constructor
MDN: the Document() constructor returns a new Document object.
Constructor — call with new Document() (no arguments).
In-memory — separate from window.document.
Factory methods — use doc.createElement, doc.createTextNode, etc.
Node family — a Document is also a Node (nodeType === 9).
ownerDocument — elements created in doc report ownerDocument === doc.
Foundation
📝 Syntax
JavaScript
new Document()
Parameters
None.
Return value
A new Document object—empty until you add nodes.
Compare
🔄 new Document() vs window.document
The global document is the current page. A constructed document is an extra tree you control in JavaScript.
JavaScript
const detached = new Document();
console.log(detached instanceof Document); // true
console.log(detached === document); // false
console.log(detached.nodeType === Node.DOCUMENT_NODE); // true
console.log(document.title); // live page title
console.log(detached.title); // "" until you set it
✅
When to use which
Use document (or window.document) for the visible page. Use new Document() when you need an isolated document for building or testing DOM structures without touching the live tree.
Compare
📄 new Document() vs createHTMLDocument()
For HTML-shaped trees, document.implementation.createHTMLDocument() is often faster to start with because it already includes <html>, <head>, and <body>.
Pick createHTMLDocument when you need head and body immediately. Pick new Document() when you want full control from an empty root.
Example 5 — Build a Mini DOM Tree
Assemble html → body → p and read markup.
JavaScript
const doc = new Document();
const html = doc.createElement("html");
const body = doc.createElement("body");
const p = doc.createElement("p");
p.textContent = "Hello from new Document()";
body.appendChild(p);
html.appendChild(body);
doc.appendChild(html);
console.log(doc.documentElement.tagName);
console.log(doc.body.textContent);
console.log(doc.documentElement.outerHTML);
The Document() constructor is Baseline Widely available across modern browsers (MDN: since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Document() constructor
Create in-memory Document objects for detached DOM trees — widely supported in modern browsers.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerNot supported · use createHTMLDocument on legacy
Not supported
Document()Excellent
Bottom line: Use new Document() for blank in-memory documents; use createHTMLDocument() when you need html/head/body immediately; use window.document for the live page.
Wrap Up
Conclusion
new Document() creates a blank, in-memory document you can build with standard DOM APIs. It is not the live page—use it when you need an isolated tree or want to learn how documents and ownerDocument relate.
Use doc.createElement so nodes belong to the right document
Prefer createHTMLDocument() when you need body immediately
Check ownerDocument when moving nodes between documents
Build detached trees for tests and templates
Remember new Document() takes no arguments
❌ Don’t
Assume new Document() changes the visible page
Confuse it with window.document
Expect doc.body before you create and attach a body
Create elements with document.createElement then append to another doc without adopting
Use IE-era patterns when new Document() is unavailable
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Document()
In-memory documents for detached DOM work.
5
Core concepts
🖱️01
Constructor
new Document()
API
📄02
Detached
not window.document
Scope
🔄03
nodeType 9
DOCUMENT_NODE
Type
📤04
Build
createElement
DOM
🎯05
Baseline
widely available
Status
❓ Frequently Asked Questions
new Document() returns a new in-memory Document object. It is a blank document you can build with createElement, appendChild, and other DOM APIs — separate from the live page document (window.document).
No. MDN marks the Document() constructor as Baseline Widely available (since April 2018). It is not Deprecated, Experimental, or Non-standard.
No. window.document is the document for the current page. new Document() creates an additional, detached document that does not affect what users see until you explicitly insert nodes from it into the live page.
document.implementation.createHTMLDocument() builds an HTML document with html, head, and body elements already present. new Document() starts empty — you assemble the tree yourself. Use createHTMLDocument when you want a ready-made HTML skeleton.
Yes. The constructor takes no arguments. Call it as new Document().
Initially null until you append a root element (often html). Once a single document element exists, document.documentElement points to that root.
Did you know?
Nodes from different documents cannot always be appended directly. Browsers may require document.importNode() or adoptNode() when moving nodes between new Document() and the live page.