What Is the DOM

Updated · 7 min read
mediumES2026javascriptdomdocumentbrowserinterview

The DOM (Document Object Model) is the browser's live, in-memory representation of a web page as a tree of objects. When the browser loads HTML, it parses the text into this tree, and JavaScript reads and changes the page entirely through it: finding a button, changing a heading's text, adding a row to a table. The DOM is not part of the JavaScript language; it is a Web API that browsers provide, standardized by the WHATWG DOM Standard. That is why document exists in a browser but not in Node.js.

In One Minute

  • The browser parses HTML into a tree of nodes; the document object is its root.
  • Every tag becomes an element node; text becomes text nodes; comments become comment nodes.
  • JavaScript changes the page by changing the DOM; the browser then updates what you see.
  • The DOM is live and can differ from the HTML source: the browser fixes errors, and scripts add and remove nodes.
  • DOM examples need a browser. In this course they are complete HTML pages, checked in headless Chromium.

From HTML to a Tree

Take this small page:

page.html
<!doctype html>
<html lang="en">
  <head>
    <title>Fee Tracker</title>
  </head>
  <body>
    <h1>Fee Tracker</h1>
    <p>Balance: <strong>$120</strong></p>
  </body>
</html>

The browser turns it into this tree. Each box is a node; indentation shows parent and child:

The DOM tree
document
└── html  (lang="en")
    ├── head
    │   └── title
    │       └── "Fee Tracker"            (text node)
    └── body
        ├── h1
        │   └── "Fee Tracker"            (text node)
        └── p
            ├── "Balance: "              (text node)
            └── strong
                └── "$120"               (text node)

The family words are used exactly as they sound: body is the parent of h1 and p; h1 and p are siblings; strong is a child of p and a descendant of body. (The real tree also contains text nodes for the line breaks and spaces between tags; the navigation lesson shows why that matters.)

The document Object

document is a global object in every browser page. It is the root of the tree and the starting point for everything you do with the DOM:

Property or methodGives
document.documentElementThe <html> element
document.head / document.bodyThe <head> and <body> elements
document.titleThe page title (readable and writable)
document.querySelector(css)The first element matching a CSS selector
document.querySelectorAll(css)Every matching element
document.createElement(tag)A new element, not yet on the page
document.html
<!doctype html>
<html lang="en">
  <head><title>Fee Tracker</title></head>
  <body>
    <h1>Fee Tracker</h1>
    <p>Balance: <strong>$120</strong></p>
    <script>
      console.log(document.title);
      console.log(document.documentElement.lang);
      console.log(document.body.children.length);
      console.log(document.querySelector("strong").textContent);
      document.title = "Fee Tracker (1 unpaid)";
      console.log(document.title);
    </script>
  </body>
</html>
Output
Fee Tracker
en
3
$120
Fee Tracker (1 unpaid)

Explanation: document.body.children.length is 3 because the <script> element itself is also a child of <body>. Setting document.title changes the text in the browser tab immediately.

Nodes and Elements

Everything in the tree is a node, but not every node is an element:

Node typenodeTypeExamplenodeName
Element1<p>, <button>"P", "BUTTON"
Text3The words inside a tag"#text"
Comment8<!-- note -->"#comment"
Document9document"#document"
nodes.html
<p id="balance">Balance: <strong>$120</strong><!-- updated nightly --></p>
<script>
  const p = document.querySelector("#balance");
  console.log(p.nodeType, p.nodeName);
  console.log(p.childNodes.length, p.children.length);
  for (const node of p.childNodes) {
    console.log(node.nodeType, node.nodeName);
  }
</script>
Output
1 P
3 1
3 #text
1 STRONG
8 #comment

Explanation: the paragraph has three child nodes (a text node, the <strong> element and a comment) but only one child element. Properties with "child" or "sibling" in their name usually come in two versions: one for all nodes (childNodes) and one for elements only (children). You'll almost always want the element version.

The DOM Is Not Your HTML

The DOM starts from your HTML, but it isn't the same thing:

  1. The browser repairs mistakes. Unclosed tags are closed, a missing <tbody> is added to tables, stray content is moved.
  2. Scripts change it. Anything JavaScript adds or removes is in the DOM, never in the source file.
  3. It reflects current state. A text input's current value is in the DOM; "View Page Source" shows only the original HTML.
repair.html
<table id="fees"><tr><td>Tuition</td><td>$400</td></tr></table>
<p>One <p>Two
<script>
  console.log(document.querySelector("#fees").innerHTML);
  console.log(document.querySelectorAll("p").length);
</script>
Output
<tbody><tr><td>Tuition</td><td>$400</td></tr></tbody>
2

Explanation: the source had no <tbody>, but the browser added one, so a selector like #fees > tr would find nothing. The second <p> automatically closed the first. When a selector doesn't find what you expect, inspect the live DOM in DevTools' Elements panel, not the source.

Where DOM Code Runs

The DOM exists only in browsers (and in browser-like environments such as jsdom, which test runners use to simulate a page). Node.js has no document:

no-dom.js▶ Try it
console.log(typeof document);
console.log(typeof globalThis.window);
Output
undefined
undefined

That's why the DOM examples in this chapter are complete HTML pages, marked as checked in Chromium rather than run with a Try it button. To try one, save it as a .html file and open it in your browser with the console open, or paste the script part into the console of any page. The same principle as in the setup lesson applies: the language is the same everywhere; the APIs depend on the runtime.

In Practice

The first DOM version of the Fee Tracker reads data from the page and writes a summary back into it. The structure (HTML) and the behavior (JavaScript) stay separate:

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Fee Tracker</title>
  </head>
  <body>
    <h1>Fee Tracker</h1>
    <ul id="fees">
      <li data-amount="400">Tuition</li>
      <li data-amount="85">Books</li>
      <li data-amount="45">Lab</li>
    </ul>
    <p id="summary">Loading...</p>
    <script>
      const items = document.querySelectorAll("#fees li");
      let total = 0;
      for (const item of items) total += Number(item.dataset.amount);
      document.querySelector("#summary").textContent = `${items.length} fees, $${total} in total`;
      console.log(document.querySelector("#summary").textContent);
    </script>
  </body>
</html>
Output
3 fees, $530 in total

The page shows "3 fees, $530 in total" where it said "Loading...". The next lessons explain each piece: querySelectorAll, dataset and textContent.

Common Mistakes

Running DOM code in Node.js. ReferenceError: document is not defined. DOM code needs a browser.

Running a script before the elements exist. A script in <head> without defer runs before <body> is parsed, so querySelector returns null. Use defer, a module, or put the script at the end of <body>.

Trusting the page source. The live DOM can differ. Inspect it in the Elements panel.

Confusing childNodes and children. childNodes includes whitespace text nodes.

Interview Corner

What is the DOM?

The Document Object Model is a language-independent API (the WHATWG DOM Standard) that represents a parsed document as a tree of node objects. Browsers build it from HTML, keep it live, and expose it to JavaScript through document. Changing the DOM changes the rendered page. It is a Web API, not part of ECMAScript.

What is the difference between a node and an element?

Node is the base interface for everything in the tree: elements, text, comments, the document itself. Element is a subtype for tags. childNodes, firstChild and nextSibling include all nodes; children, firstElementChild and nextElementSibling include only elements.

Practice

Challenge: Write a page with a list of three students and a script that logs how many <li> elements there are and the text of the last one.

Show solution
students.html
<ul id="students">
  <li>Navika</li>
  <li>Srihitha</li>
  <li>Advitha</li>
</ul>
<script>
  const items = document.querySelectorAll("#students li");
  console.log(items.length);
  console.log(items[items.length - 1].textContent);
</script>
Output
3
Advitha

Quick check: The DOM

3 questions
1What is the DOM?
2A paragraph contains text, a strong element and a comment. What is its children.length?
3Why might #fees > tr find nothing in a table written without tbody?

Key Takeaways

TermMeaning
DOMThe page as a tree of objects
documentThe root of the tree, a global in browsers
NodeAny item in the tree
ElementA node for a tag
children / childNodesChild elements / all child nodes

Was this tutorial helpful?

Lesson 67 of 139