JavaScript: Introduction to the DOM

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Gray🏷 Category: JavaScript Tutorial

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
<!-- Paragraph -->
<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 What exactly is the DOM, and how does it relate to JavaScript?
A The DOM (Document Object Model) is a node tree that the browser generates after parsing HTML, and document is the root entry point of this tree. It is standardized by the W3C and is not part of JavaScript itself; JS only reads and modifies this tree through APIs provided by the browser. That's why there is no DOM in Node.js, since there is no HTML document environment there.
Q When traversing elements with childNodes, why do many empty nodes show up?
A Line breaks and indentation between HTML tags are parsed as text nodes, so nodes containing whitespace get mixed into childNodes. If you only want element nodes, use children instead, or select elements directly with querySelectorAll. You can check the node type with nodeType === 1.
Q When working with the DOM, how do I choose between getElementById, querySelector, and children?
A To get a single element by id, use getElementById, which is the fastest and most direct option; when you need CSS selector syntax (such as .item > a), use querySelector/querySelectorAll. When you already have a parent element and only want to iterate over its child elements, use children, which is simpler than filtering childNodes.

📖 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.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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