The page is a tree of objects
When a browser loads an HTML file, it doesn’t just display text. It parses the HTML and builds an in-memory tree of objects called the Document Object Model (DOM). Every HTML element becomes a JavaScript object you can read, modify, and delete.The document object
document is your entry point to the DOM. It’s a global object available in any browser JavaScript — you don’t need to import anything.
document is only available in the browser. If you try to use it in Node.js, you’ll get ReferenceError: document is not defined. This is a browser-only API.What can you do with the DOM?
The DOM lets JavaScript interact with the page:| Action | Example |
|---|---|
| Find elements | document.querySelector(".user-card") |
| Read content | element.textContent |
| Change content | element.textContent = "New text" |
| Change styles | element.style.color = "red" |
| Add elements | parent.appendChild(newElement) |
| Remove elements | element.remove() |
| Listen for events | button.addEventListener("click", handler) |
How this connects to React
Here’s the key insight: React is a DOM manipulation library. Everything React does — rendering components, updating the UI, handling events — ultimately translates to DOM operations.- Debug React applications (the Elements panel shows the real DOM)
- Understand why React re-renders
- Work with libraries that touch the DOM directly
- Handle edge cases React can’t cover
DOM vs HTML
The DOM is not your HTML file. The browser builds the DOM from your HTML, but they can differ:h1 and p inside #app, but the HTML file still just says <div id="app"></div>. The DOM is the live, current state of the page.
When you “View Source” in the browser, you see the original HTML. When you inspect with DevTools, you see the current DOM. They’re often different because JavaScript modifies the DOM after the page loads.
What’s next?
Now that you know what the DOM is, let’s learn the tool you’ll use every day to inspect and debug it — browser DevTools.Browser DevTools
Inspect, debug, and test your JavaScript in the browser