JavaScript Document Constructor

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Web API constructor

What You’ll Learn

The Document() constructor creates a new in-memory Document. 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

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.

JavaScript
const doc = new Document();
console.log(doc.nodeType); // 9 (DOCUMENT_NODE)
console.log(doc === document); // false
💡
Beginner tip

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).

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.

📝 Syntax

JavaScript
new Document()

Parameters

None.

Return value

A new Document object—empty until you add nodes.

🔄 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.

📄 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>.

JavaScript
const blank = new Document();
const htmlDoc = document.implementation.createHTMLDocument("My title");

console.log(blank.documentElement);              // null
console.log(htmlDoc.documentElement.tagName);  // "HTML"
console.log(htmlDoc.body.tagName);             // "BODY"

⚡ Quick Reference

GoalCode
Create blank documentconst doc = new Document()
Check node typedoc.nodeType === 9
Create element in docdoc.createElement("p")
HTML skeleton shortcutdocument.implementation.createHTMLDocument()
Live page documentwindow.document
MDN statusBaseline Widely available (Apr 2018)

🔍 At a Glance

Four facts about new Document().

Kind
constructor

Web API

Returns
Document

in-memory tree

Args
none

zero parameters

Status
baseline

Widely available

Examples Gallery

Examples follow MDN Document() and common patterns for detached DOM documents.

📚 Getting Started

Create a Document and inspect its type.

Example 1 — Basic new Document()

Create an empty in-memory document and read its node type.

JavaScript
const doc = new Document();

console.log(doc.constructor.name);
console.log(doc.nodeType);
console.log(doc.nodeName);
console.log(doc instanceof Document);
console.log(doc.documentElement);
Try It Yourself

How It Works

nodeType 9 is Node.DOCUMENT_NODE. documentElement is null until a root element is attached.

Example 2 — createElement on a Detached Document

Elements belong to the document that created them.

JavaScript
const doc = new Document();
const p = doc.createElement("p");
p.textContent = "Built off-screen";

console.log(p.ownerDocument === doc);
console.log(p.textContent);
console.log(doc.body); // null — no <body> yet
Try It Yourself

How It Works

ownerDocument links each node to its document. Without a <body>, doc.body stays null even after you create elements.

📈 Compare & Build

Contrast with the live page and assemble a mini tree.

Example 3 — Not the Same as window.document

The live page document and a constructed one are different objects.

JavaScript
const detached = new Document();

console.log(detached === document);
console.log(detached.nodeType === document.nodeType);
console.log(Boolean(document.documentElement));
console.log(detached.documentElement);
Try It Yourself

How It Works

Both are Document nodes (nodeType 9), but only window.document represents the loaded page and already has a documentElement.

Example 4 — Compare with createHTMLDocument()

HTML factory gives you a ready-made skeleton.

JavaScript
const blank = new Document();
const htmlDoc = document.implementation.createHTMLDocument("Demo");

console.log(blank.documentElement);
console.log(htmlDoc.documentElement.tagName);
console.log(htmlDoc.title);
console.log(htmlDoc.body !== null);
Try It Yourself

How It Works

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 htmlbodyp 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);
Try It Yourself

How It Works

After appending html, documentElement and body shortcuts work like on a normal HTML document. See appendChild().

🚀 Common Use Cases

  • Unit tests that need a real Document without a browser page.
  • Building HTML fragments before inserting them into the live DOM.
  • Learning how ownerDocument ties nodes to a document.
  • Prototyping DOM APIs in the console without changing the visible page.
  • Comparing factory methods (new Document vs createHTMLDocument).

🔧 How It Works

1

Call new Document()

Engine allocates an empty Document (nodeType 9).

Create
2

Create nodes with doc.create*

Elements and text nodes belong to this document.

Factory
3

appendChild builds the tree

Attach a root element; shortcuts like body may appear.

Assemble
4

Use or import into live page

Serialize, clone, or move nodes into window.document.

📝 Notes

  • Baseline Widely available (MDN, since April 2018).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • The constructor accepts no parameters.
  • For a ready HTML skeleton, prefer document.implementation.createHTMLDocument().
  • Related: ownerDocument, appendChild(), nodeType, Text(), JavaScript hub.

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Not 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.

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.

Continue with ownerDocument, appendChild(), nodeType, or the JavaScript hub.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about Document()

In-memory documents for detached DOM work.

5
Core concepts
📄 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.

Next: Node.ownerDocument

Learn which document owns each node in the DOM tree.

ownerDocument →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful