What is the DOM?
The DOM (Document Object Model) is the browser's live, tree-shaped object representation of an HTML document; JavaScript reads and changes the page through it, and the browser re-renders whatever changes.
The tree
The browser parses HTML into nodes: the document at the root, then element nodes (<html>, <body>, <div>), text nodes, comment nodes and attributes. Each element node is an object with properties (id, className, value), methods (append, remove, addEventListener) and relationships (parentElement, children, nextElementSibling).
DOM vs HTML source
The DOM is not the HTML file. The browser fixes invalid markup, adds missing tags (like <tbody>), and JavaScript changes the DOM after load. DevTools' Elements panel shows the current DOM, while "View Source" shows the original HTML.
What you do with it
- Find elements:
querySelector,querySelectorAll,getElementById. - Change content and structure:
textContent,append,remove,replaceWith. - Change appearance:
classList,style. - React to users:
addEventListener.
Performance
Reading layout properties (offsetHeight, getBoundingClientRect) after writes forces the browser to recalculate layout. Batch reads and writes, and build many nodes off-screen (e.g. in a DocumentFragment) before inserting them.
Frameworks
React, Vue, Svelte and Angular manage the DOM for you and update only what changed; you rarely touch it directly, but knowing it explains what they do under the hood.
Code example
1document.body.innerHTML = `2 <main id="app">3 <h1 class="title">Shopping list</h1>4 <ul id="list"><li>Milk</li><li>Bread</li></ul>5 </main>`;67// The DOM is a tree of objects8const list = document.querySelector("#list");9console.log(list.tagName, list.children.length); // UL 210console.log(list.parentElement.id); // app11console.log(list.firstElementChild.textContent); // Milk1213// Change it: the page updates immediately14const item = document.createElement("li");15item.textContent = "Eggs";16list.append(item);17document.querySelector(".title").textContent = "Shopping list (3 items)";1819// Walk the tree20function printTree(node, depth = 0) {21 console.log(`${" ".repeat(depth)}<${node.tagName.toLowerCase()}>`);22 for (const child of node.children) printTree(child, depth + 1);23}24printTree(document.querySelector("#app"));
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.