JavaScript Document doctype Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Document.doctype is a read-only property that returns a DocumentType object for the document’s Document Type Declaration (DTD), or null if none exists (MDN).

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.doctype. Start with this line.

JavaScript
document.doctype
CallWhat it does
document.doctypePrimary signature.

Built into the browser. Read or assign document.doctype — no import required.

Library declarations
JavaScript
document.doctype

Parameters

ParameterDescription
.publicId / .systemIdOptional. Legacy / XML DTD identifiers

Return Value

Return ValueDescription
*DocumentType or null

JavaScript Examples

Example 1 — Read doctype Safely

Null-check, then log the type name.

JavaScript
const dt = document.doctype;
if (dt) {
  console.log(dt.name);  // "html" on typical HTML5 pages
} else {
  console.log("no doctype");
}
Try It Yourself

How It Works

  1. Read the property. You get a DocumentType or null.
  2. Guard against null. Avoids errors when no DTD exists.
  3. Use .name. HTML documents report "html".
  4. HTML5 habit. Pages should start with <!DOCTYPE html>.

Example 2 — Log name, publicId, systemId

MDN’s field trio on the DocumentType object.

JavaScript
const dt = document.doctype;
if (!dt) {
  console.log("no doctype");
} else {
  console.log(dt.name);
  console.log(dt.publicId);
  console.log(dt.systemId);
}
Try It Yourself

How It Works

  1. Confirm the node exists. Skip field access when doctype is null.
  2. Read name. The document type name ("html" for HTML).
  3. Read IDs. publicId / systemId are often empty on HTML5.
  4. Older DTDs differ. XHTML-era doctypes may fill those ID strings.

Example 3 — Pair with compatMode

Doctype presence and rendering mode are related ideas.

JavaScript
const dt = document.doctype;
console.log(dt ? dt.name : "no doctype");
console.log(document.compatMode);
// "CSS1Compat" = standards mode
// "BackCompat" = quirks mode
Try It Yourself

How It Works

  1. Print the type name. Confirms a doctype node is present.
  2. Read compatMode. Shows standards vs quirks rendering.
  3. Modern pages. Expect "html" + "CSS1Compat".
  4. Fix in markup. Add <!DOCTYPE html> — you cannot set doctype via this property.

Browser Support

Document.doctype is Baseline Widely available (MDN: since July 2015). Returns a DocumentType or null.

✓ Baseline · Widely available

Document.doctype

Read-only DocumentType for the document’s DTD — supported across modern browsers.

99% Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
doctype Excellent

Bottom line: Safe for production diagnostics. Always null-check, and keep <!DOCTYPE html> in your HTML files for standards mode.

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 not Deprecated, Experimental, or Non-standard.
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).

Next: documentElement

Learn the Document property that returns the root <html> element.

documentElement tutorial →

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.

11 people found this page helpful