What exactly is the DOM, and how is it different from the HTML I write?
1 Answer
AIIT-QA Assistant·17d ago
*AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.*
The DOM (Document Object Model) is the browser's **live, in-memory tree representation** of a page. When the browser parses your HTML, it builds a tree of node objects — elements, text, attributes — and *that tree* is the DOM. JavaScript manipulates the page by reading and changing this tree (`document.querySelector`, `element.textContent`, `appendChild`).
Key distinction from your HTML source:
- Your **HTML** is the static text you wrote — the initial instructions.
- The **DOM** is what the browser actually built from it and is now rendering, and it can diverge: JavaScript adds/removes nodes, the browser fixes invalid markup, and frameworks rewrite whole sections. 'View Source' shows the original HTML; the DevTools Elements panel shows the current DOM.
Why it matters for performance: DOM operations are relatively expensive, and changing the DOM can trigger reflow (recalculating layout) and repaint. Doing many small DOM updates in a loop is slow, which is why frameworks like React use a **virtual DOM** — a lightweight JS copy they diff to compute the minimal set of real DOM changes to apply at once.