JavaScript: Introduction to the DOM

Last updated: 2026-10-13

The DOM is the only bridge between JavaScript and web pages. Without the DOM, JS would be nothing more than a pure computation language. With the DOM, JS can "see" pages, modify pages, and bring them to life.

Related DOM Methods:document.getElementById() document.querySelector() document.createElement() querySelectorAll class

1. The DOM Tree and document Object

(1) What Is the DOM?

DOM stands for Document Object Model. When a browser loads HTML, it doesn't store it as plain text — it translates it into an object tree. Each HTML tag becomes an object (called a node), and the nesting relationships between tags become parent-child relationships in the tree.

Think of the DOM as an upside-down tree: at the very top is the root (document), branching down into limbs (html, head, body), and further down into leaves (div, p, span...).

(2) DOM Tree Structure

The DOM tree of a simple HTML page looks roughly like this:

HTML
document
  └── html
       ├── head
       │    ├── meta
       │    └── title
       └── body
            ├── h1
            ├── p
            └── div
                 └── span

(3) Node Types

There are three core node types in the DOM:

Node Type Description nodeType Value
Element node An HTML tag, e.g. p, div 1
Text node Text content inside a tag 3
Attribute node A tag's attribute, e.g. class, id 2 (deprecated for general use)
HTML
<p class="intro">Hello World</p>

In this single line: <p> is an element node, class="intro" is an attribute node, and Hello World is a text node. In everyday development, element nodes are what you'll work with the most.

(4) The document Object

document is the entry point to the DOM — the browser provides this global object automatically. Through it you can:

(5) What the DOM Does

JS itself cannot "see" a web page — it can only compute. The DOM provides the "eyes" and "hands":

Without the DOM, JS and HTML would be two completely separate things. The DOM connects them.


▶ Example: Viewing the DOM Structure

▶ Try it Yourself

▶ Example: Exploring the document Object

▶ Try it Yourself

▶ Example: Identifying Node Types

▶ Try it Yourself

Related DOM Methods:document.getElementById() document.querySelector() document.createElement() querySelectorAll class

❓ FAQ

Q Is the DOM part of JavaScript?
A No. The DOM is a standard defined by the W3C. JavaScript merely provides APIs to interact with it. Node.js has no DOM because it isn't a browser environment. Confusing DOM and JS is a common beginner misconception.
Q Why are there empty text nodes in childNodes?
A Line breaks and indentation in HTML are parsed as text nodes in the DOM. For example, the whitespace between two tags becomes a text node (containing a newline character). If you only want element nodes, use children instead of childNodes.
Q Does modifying the DOM affect page performance?
A Yes. Every DOM modification can trigger a browser reflow and repaint. Frequent DOM manipulation is a performance killer. Best practice: build all your changes in memory first, then write to the DOM in one go.

📖 Summary

📝 Exercises

  1. Open the browser DevTools (F12), type console.dir(document) in the Console, and explore the document object's properties and methods. List 5 that you recognize or find interesting.
  2. Write code that gets the body element of the current page, prints its childElementCount (number of child elements), then uses children to loop through all child elements and print each one's tag name.
  3. Explain the difference between "element nodes" and "text nodes". Given <div>Hello <b>World</b></div>, draw its node tree structure and label the type of each node.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏