Types of DOM
"Types of DOM" usually refers to how the Document Object Model standard was divided into parts for different kinds of documents: the Core DOM, the XML DOM and the HTML DOM. The term is also used for the different types of nodes inside the tree, and people often meet related ideas like the Shadow DOM and the virtual DOM. Sorting these terms out early makes later DOM lessons much easier to follow.
Core DOM, XML DOM and HTML DOM
The original W3C specifications split the DOM into layers:
| Part | What it covers | Examples of what it provides |
|---|---|---|
| Core DOM | The shared model for any structured document | Nodes, parents and children, appendChild, getAttribute |
| XML DOM | Core DOM applied to XML documents | Reading an RSS feed or an XML response with DOMParser |
| HTML DOM | Extra objects and properties for HTML pages | document.body, document.forms, input.value, img.src |
The Core DOM knows only about generic elements and attributes. The HTML DOM adds convenient, HTML-specific features on top. For example, every element can use the core method getAttribute('href'), but an <a> element in the HTML DOM also offers an href property that gives you the full resolved URL.
Today, browsers follow the DOM and HTML Living Standards maintained by WHATWG, which merge these ideas. The three-way split is still a useful way to understand where a feature comes from.
Types of nodes in the tree
Whatever kind of document it is, the DOM is made of nodes, and each node has a type, available through its nodeType property:
- Document node (
nodeType9): the root, which is thedocumentobject itself. - Element node (1): each tag, such as
<p>or<div>. - Text node (3): the text inside elements, including whitespace between tags.
- Comment node (8): HTML comments.
- Attribute nodes exist in the specification but are not children in the tree; you work with attributes through element methods.
<ul id="menu">
<li>Home</li>
<!-- more items later -->
</ul>
<script>
const menu = document.getElementById('menu');
menu.childNodes.forEach((node) => {
console.log(node.nodeType, node.nodeName);
});
// 3 "#text" (whitespace)
// 1 "LI"
// 3 "#text"
// 8 "#comment"
// 3 "#text"
console.log(menu.children.length); // 1, elements only
</script>
The whitespace text node trap
That output explains a classic beginner bug. Properties like childNodes, firstChild and nextSibling include every node, so the line breaks between your tags appear as text nodes. Code that expects menu.firstChild to be the first <li> gets a whitespace text node instead. Use the element-only versions: children, firstElementChild and nextElementSibling.
Shadow DOM and virtual DOM
Two more terms often appear alongside "types of DOM", though they are not parts of the original split:
- Shadow DOM is a real browser feature. It lets a component attach a hidden, separate DOM tree to an element, so its styles and markup do not leak out or get affected by the page. Built-in controls like
<video>use it, and web components can too. - Virtual DOM is not a browser feature at all. It is a technique used by libraries such as React: a lightweight JavaScript copy of the UI that is compared with the previous copy, so only the necessary real DOM changes are made.
What to remember
The Core DOM gives the shared tree model, the HTML DOM adds page-specific conveniences, and the XML DOM applies the same model to XML. Inside any of them, elements, text and comments are different node types, and choosing element-only properties saves you from whitespace surprises.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.