JavaScript Document doctype Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.doctype is a read-only instance property that returns a DocumentType object for the document’s Document Type Declaration (DTD), or null if none exists. Learn name, publicId, systemId, how HTML5 doctypes look, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

DocumentType

03

Or

null

04

HTML5

name "html"

05

Edit?

Not via DOM

06

Status

Baseline widely

Introduction

At the top of almost every HTML file you write <!DOCTYPE html>. That line is the document type declaration. It tells the browser which kind of document rules to use (and historically avoided quirks mode).

MDN: document.doctype is the read-only DocumentType object for that declaration. If the document has no DTD, the property is null. You cannot edit the doctype through the DOM (DOM Level 2 does not support editing it).

💡
HTML5 in practice

For <!DOCTYPE html>, expect doctype.name === "html" with empty publicId and systemId. Older XHTML/HTML4 doctypes carried longer public/system identifiers.

Related Document tutorials: dir, compatMode, Document constructor.

Understanding Document.doctype

A read-only instance property on Document. Value is a DocumentType node or null.

  • name — root type name (e.g. "html").
  • publicId — public identifier string (often empty in HTML5).
  • systemId — system identifier string (often empty in HTML5).
  • internalSubset — internal DTD subset string when present (MDN example).
  • Not editable — change the source markup; do not assign to document.doctype.

📝 Syntax

JavaScript
document.doctype

Value

A DocumentType object, or null if there is no DTD (MDN).

MDN-style inspection

JavaScript
const doctypeObj = document.doctype;

console.log(`doctypeObj.name: ${doctypeObj.name}`);
console.log(`doctypeObj.internalSubset: ${doctypeObj.internalSubset}`);
console.log(`doctypeObj.publicId: ${doctypeObj.publicId}`);
console.log(`doctypeObj.systemId: ${doctypeObj.systemId}`);

⚡ Quick Reference

GoalCode / note
Get DocumentTypedocument.doctype
Safe namedocument.doctype?.name
HTML5 checkdocument.doctype?.name === "html"
Missing doctypedocument.doctype === null
Standards modedocument.compatMode === "CSS1Compat"
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.doctype.

Type
DocumentType | null

Or null

Access
read-only

No setter

HTML5
name: "html"

Typical

Status
baseline

Standard API

📋 DocumentType properties (MDN)

PropertyMeaningHTML5 example
nameDeclared root name"html"
publicIdFormal public identifier""
systemIdSystem identifier / URI""
internalSubsetInternal subset stringUsually empty / unused in HTML5

Examples Gallery

Examples follow MDN Document: doctype. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read the DocumentType and log its fields (MDN).

Example 1 — Read document.doctype

Confirm the page has a doctype node.

JavaScript
console.log(document.doctype);
// DocumentType { name: "html", ... } on a normal HTML5 page
Try It Yourself

How It Works

The browser exposes the parsed doctype as a node at the start of the document tree.

Example 2 — Inspect name / publicId / systemId (MDN)

MDN’s example logging the main DocumentType fields.

JavaScript
const doctypeObj = document.doctype;

console.log(`doctypeObj.name: ${doctypeObj.name}`);
console.log(`doctypeObj.internalSubset: ${doctypeObj.internalSubset}`);
console.log(`doctypeObj.publicId: ${doctypeObj.publicId}`);
console.log(`doctypeObj.systemId: ${doctypeObj.systemId}`);
Try It Yourself

How It Works

Guard with if (document.doctype) before reading fields if the document might lack a DTD.

📈 HTML5 Checks, Null & Mode

Practical diagnostics for modern pages.

Example 3 — Detect a Simple HTML5 Doctype

Name is html with empty public/system ids.

JavaScript
const dt = document.doctype;
const isHtml5Like =
  !!dt &&
  dt.name.toLowerCase() === "html" &&
  !dt.publicId &&
  !dt.systemId;

console.log("HTML5-like doctype:", isHtml5Like);
Try It Yourself

How It Works

This heuristic matches common HTML5 pages; XML/SVG documents may differ.

Example 4 — Null-Safe Access

Created documents may not expose a doctype.

JavaScript
const fresh = document.implementation.createHTMLDocument("Scratch");

console.log("Main has doctype:", document.doctype !== null);
console.log("Fresh doctype:", fresh.doctype);
console.log("Fresh name:", fresh.doctype?.name ?? "(none)");
Try It Yourself

How It Works

MDN: the property returns null when no DTD is associated. Always optional-chain in shared helpers. (createHTMLDocument often includes an HTML doctype in modern browsers.)

Example 5 — Pair with compatMode

Doctype presence and rendering mode are related diagnostics.

JavaScript
const info = {
  hasDoctype: document.doctype !== null,
  name: document.doctype?.name ?? null,
  compatMode: document.compatMode
};

console.log(JSON.stringify(info, null, 2));
Try It Yourself

How It Works

See also compatMode for quirks vs standards rendering.

🚀 Common Use Cases

  • Diagnostics — confirm a page shipped with a doctype.
  • Support tooling — log name / ids beside compatMode.
  • Teaching HTML — show what <!DOCTYPE html> becomes in the DOM.
  • XML / legacy docs — inspect public/system identifiers when present.
  • Not for rewriting doctypes — edit the HTML source instead (MDN).
  • Standards hygiene — pair with ensuring CSS1Compat mode.

🧠 How the Doctype Becomes a Node

1

Source includes a declaration

<!DOCTYPE html> (or a longer legacy DTD).

Markup
2

Parser builds DocumentType

Creates a node with name / publicId / systemId.

Parse
3

Attached to the Document

Available as document.doctype.

DOM
4

Scripts read, not rewrite

Inspect for diagnostics; fix markup at the source if needed.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Read-only DocumentType or null.
  • DOM Level 2 does not support editing the document type declaration (MDN).
  • Always null-check before reading .name and related fields.
  • Related: compatMode, dir, Document constructor.

Universal Browser Support

Document.doctype is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.doctype

Read-only DocumentType for the document's DTD — or null if none is associated.

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 Supported (legacy)
Full support
Document.doctype Excellent

Bottom line: Use document.doctype to inspect the DocumentType. Always include in HTML source — you cannot set the doctype from JavaScript.

Conclusion

Document.doctype exposes the parsed Document Type Declaration as a DocumentType node. On modern HTML5 pages that usually means name: "html". Read it for diagnostics; fix doctypes in your HTML source, not with a JavaScript assignment.

Continue with documentElement, compatMode, dir, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Start HTML files with <!DOCTYPE html>
  • Null-check document.doctype before reading fields
  • Log doctype with compatMode in support reports
  • Use optional chaining (doctype?.name)
  • Teach HTML5’s short doctype to beginners

❌ Don’t

  • Try to assign document.doctype
  • Assume every Document has a doctype
  • Ship pages without a doctype (risk of quirks mode)
  • Confuse doctype with contentType (MIME)
  • Rely on legacy public/system ids for modern HTML5 apps

Key Takeaways

Knowledge Unlocked

Five things to remember about document.doctype

Read-only DocumentType — or null when missing.

5
Core concepts
02

Status

baseline

Standard
🔒03

Access

read-only

DOM
⚠️04

Missing

null

MDN
🎯05

HTML5

name html

Typical

❓ Frequently Asked Questions

A DocumentType object representing the Document Type Declaration (DTD) associated with the current document, or null if there is no DTD.
No. MDN marks Document.doctype as Baseline Widely available (since July 2015). It is a standard read-only Document instance property.
For a normal HTML5 page with <!DOCTYPE html>, doctype.name is usually "html". publicId and systemId are typically empty strings.
No. MDN notes that DOM Level 2 does not support editing the document type declaration. The property is read-only.
When there is no DTD associated with the document — for example some programmatically created documents without a doctype node.
Missing or incorrect doctypes historically triggered quirks mode. Modern pages should include <!DOCTYPE html>. You can also check document.compatMode (CSS1Compat vs BackCompat).
Did you know?

Before HTML5’s short <!DOCTYPE html>, authors copied long SGML-style doctypes with public and system identifiers. Those still appear as non-empty publicId / systemId on document.doctype when you open old pages.

Next: documentElement

Learn how to access the document’s root Element (usually <html>).

documentElement →

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.

6 people found this page helpful