MyInternships.in

HTML DOM

HTML DOM doctype Property

The doctype property returns the Document Type Declaration associated with the document as a DocumentType node. For a modern page this is the <!DOCTYPE html> declaration at the top of the file.


Definition and Usage

document.doctype returns the Document Type Declaration (the <!DOCTYPE ...> line) as a DocumentType node, or null if the document has no doctype. The node exposes a name property, which is "html" for standard HTML5 pages.

The property is read-only. It is mostly used to confirm that a page is running in standards mode, which happens when a valid doctype is present.

Syntax

document.doctype

Properties of the returned node

NameDescription
nameThe name of the document type, usually "html"
publicIdThe public identifier (empty for HTML5)
systemIdThe system identifier (empty for HTML5)

Example

Click the button to read the doctype node and print its name into the output box.

Read the document doctype
Example
<!DOCTYPE html>
<html>
<body>
  <button onclick="showType()">Show doctype</button>
  <p id="out"></p>

  <script>
    function showType() {
      const dt = document.doctype;
      document.getElementById("out").textContent =
        dt ? "Doctype name: " + dt.name : "No doctype found";
    }
  </script>
</body>
</html>

More Examples

You can reconstruct the doctype string from the node. This example builds a readable version of the declaration.

Rebuild the doctype declaration string
Example
<!DOCTYPE html>
<html>
<body>
  <p id="out"></p>
  <script>
    const dt = document.doctype;
    let text = "<!DOCTYPE " + dt.name;
    if (dt.publicId) text += ' PUBLIC "' + dt.publicId + '"';
    text += ">";
    document.getElementById("out").textContent = text;
  </script>
</body>
</html>
⚠️

If a page omits the doctype, document.doctype returns null and the browser may switch to quirks mode, which changes how CSS is rendered.

Key Takeaways

  • document.doctype returns the <!DOCTYPE> declaration as a node.
  • For HTML5 pages the node's name is "html".
  • It returns null when no doctype is present.
  • A valid doctype keeps the browser in standards mode.

Related HTML Topics

Keep learning with these closely related tutorials.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships