What is the DOM tree?
The DOM (Document Object Model) is the object model of an HTML document, that is, the structure of a web page as a tree of objects that JavaScript can work with.
Example:
Here is a simple HTML page:
<html>
<body>
<h1>Hello</h1>
<p>World</p>
</body>
</html>When the browser loads this page, it converts the HTML into a DOM tree:
Document
└── html
└── body
├── h1
│ └── "Hello"
└── p
└── "World"In other words, the DOM is the browser's in-memory representation of the HTML page, where each tag is an object, and the nesting of tags is a hierarchy (a tree).
How the DOM differs from HTML
| Difference | HTML | DOM |
|---|---|---|
| What it is | Text code (markup) | The document's object model |
| Where it exists | In a file (.html) | In the browser's memory |
| Format | Strings (tags, attributes) | Objects (nodes, properties, methods) |
| Changed by JS | No (HTML is a static file) | Yes, the DOM can be changed dynamically |
| Example | <p>Hello</p> | document.querySelector('p').textContent = 'Hi' |
In other words:
- HTML is the source code of the page.
- DOM is the live structure the browser and JavaScript work with.
Types of nodes in the DOM
Every element of the DOM tree is a node. There are several types of nodes:
| Node type | Example | Description |
|---|---|---|
Document | document | The root of the tree |
Element | <div>, <p> | HTML tags |
Text | "Hello" | Text inside tags |
Comment | <!-- comment --> | Comments |
Attribute | class="title" | Element attributes |
Working with the DOM through JavaScript
1. Getting elements
document.getElementById('id');
document.getElementsByClassName('class');
document.getElementsByTagName('p');
document.querySelector('.class');
document.querySelectorAll('p');The modern ways are
querySelector()andquerySelectorAll().
2. Changing content
const el = document.querySelector('h1');
el.textContent = 'Hello, world!'; // Changes the text
el.innerHTML = '<em>Hello!</em>'; // Inserts HTML3. Changing attributes and styles
el.id = 'title';
el.setAttribute('data-role', 'main');
el.style.color = 'red';
el.classList.add('active');4. Creating and adding elements
const newDiv = document.createElement('div');
newDiv.textContent = 'I am a new block';
document.body.appendChild(newDiv);You can create, insert, and remove nodes, thereby dynamically changing the page.
5. Removing elements
const p = document.querySelector('p');
p.remove();6. Navigating the tree
const el = document.querySelector('div');
el.parentElement; // parent
el.children; // child elements
el.firstElementChild; // first descendant
el.nextElementSibling; // adjacent elementExample: dynamically changing a page
<ul id="list">
<li>JS</li>
<li>HTML</li>
</ul>
<script>
const list = document.getElementById('list');
const li = document.createElement('li');
li.textContent = 'CSS';
list.appendChild(li);
</script>Now the list will become:
javascriptJS HTML CSS
Summary
| Parameter | HTML | DOM |
|---|---|---|
| What it is | Document markup | An in-memory structure |
| Format | Text (tags) | Objects |
| Changed by JS | No | Yes |
| Where it exists | On disk (a file) | In the browser (in memory) |
| Interaction | Only parsed | You can read, change, remove nodes |
In short:
The DOM is the "live" version of HTML that JavaScript works with. It represents the page as an object tree, where nodes can be read, changed, and removed.
A formula to remember it by: HTML is text, DOM is a structure of objects, JS is the tool for managing that structure.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.