The DOM (Document Object Model) is the browser's live, in-memory tree of objects representing the page, built by parsing HTML (or created by script). Each piece of the document is a node: the Document at the root, then element nodes, text nodes, comments and so on. JavaScript reads and changes the page through the DOM API, and the browser re-renders to reflect those changes. The DOM is not the HTML source and not what's painted; it sits between them.
<!DOCTYPE html>
<html lang="en">
<head><title>Shop</title></head>
<body>
<h1 class="title">Cart</h1>
<!-- items -->
<ul id="items"><li>Apple</li></ul>
</body>
</html>
Document
├── DocumentType (html)
└── <html>
├── <head>
│ └── <title> → Text "Shop"
└── <body>
├── Text "\n " ← whitespace is a text node too
├── <h1 class="title"> → Text "Cart"
├── Comment " items "
└── <ul id="items">
└── <li> → Text "Apple"
| Node type | nodeType | Example |
|---|---|---|
| Element | 1 | <div>, <svg> |
| Text | 3 | text inside elements, including whitespace |
| Comment | 8 | <!-- … --> |
| Document | 9 | document |
| DocumentType | 10 | <!DOCTYPE html> |
| DocumentFragment | 11 | off-tree container, <template>.content, shadow roots |
Interfaces form an inheritance chain: HTMLButtonElement → HTMLElement → Element → Node → EventTarget. That's why every element can do addEventListener (EventTarget), appendChild (Node), querySelector (Element) and has .hidden (HTMLElement).
const list = document.getElementById('items');
list.parentElement; // <body>
list.children; // HTMLCollection of element children (live)
list.childNodes; // NodeList incl. text/comment nodes (live)
list.firstElementChild; // <li>
list.previousElementSibling; // <h1>
document.documentElement; // <html>
list.closest('body'); // nearest ancestor (or self) matching a selector
Prefer the *Element* variants (children, firstElementChild) to skip whitespace text nodes.
display: none elements, but includes pseudo-elements).DOM changes invalidate style/layout; the browser batches the work until the next frame. Reading layout properties (offsetHeight, getBoundingClientRect) right after writes forces a synchronous layout (layout thrashing), so batch reads before writes.
MutationObserver lets you react to changes.<tbody>, closing tags), and frameworks build it entirely at runtime. DevTools' Elements panel shows the DOM, while "View source" shows the original HTML.EventTarget.Spotted a mistake or a rights concern? Report an issue