LearnThatStack Ace your next interview
JavaScript · question
Question 12 of 126

What is the DOM?

beginner
← All JavaScript questions
Re-explain
Visual ↓

The Document Object Model (DOM) is the programming interface for an HTML document. It represents the page as a tree of objects that JavaScript can read and change.

The browser parses the HTML and builds a node for each element, attribute and piece of text. The document object is the root of that tree. Scripts start from it to find and change nodes, or to add and remove them, and the screen updates to match.

The DOM is not the HTML source. It reflects the page as it is now, including every change made by scripts.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
The DOM: HTML parsed into a live tree of nodes
const body = document.body;
const p =
document.createElement('p');
p.textContent = 'Sale!';
body.append(p);
HTML sourcenever changes
head<title>Shop</title>
body<h1>Hello</h1>
DOM treethe page right now
"Shop"#texttitlehead"Hello"#texth1bodyhtmldocumentroot
Variableswhat the script holds
empty
1step

The browser parses the HTML into a tree with document at the root. The word Hello becomes a text node of its own, inside h1.

1 / 6

The DOM is a tree of nodes under document, and text gets its own nodes. Scripts change that tree, so it can differ from the HTML source.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • How is a DOM property different from the HTML attribute with the same name? The attribute is the value written in the markup. The property is the node's current state, like an input's value after the user types.
  • Why can frequent DOM changes make a page slow, and how do you keep the cost down? Changes can force style, layout and paint work. Batch writes, avoid reading layout between writes, and build off-screen in a DocumentFragment.
  • A script adds an element after load. Will you see it in view-source? Where would you look instead? View-source shows the HTML the server sent. The DevTools Elements panel shows the live DOM, including changes made by scripts.
  • What is the difference between DOMContentLoaded and load, and which one do you wait for to touch the DOM? DOMContentLoaded fires once the HTML is parsed and the tree is built. load also waits for images and stylesheets. DOM work needs only the first.
  • How would you handle clicks on 1,000 list items without adding 1,000 listeners? Put one listener on the parent. Events bubble up, so check event.target, often with closest(), to find which item was clicked.

What you can say

  1. The DOM, or Document Object Model, is the browser's representation of a page as a tree of objects, and it's the interface JavaScript uses to read and change that page.
  2. When the browser parses the HTML, it builds a node for every element, attribute and piece of text, and links them into that tree.
  3. The document object sits at the root, so a script starts from it to find nodes, change them, or add and remove them.
  4. When a script changes the tree, the browser updates the screen to match.
  5. The DOM is not the HTML source but the page as it is right now, so it includes every change scripts have made and can differ from what view-source shows.

Weak answers to avoid

  • Says the DOM is just the HTML HTML is the text the server sent. The DOM is the live tree the browser built from it, and scripts keep changing it, so the two can differ after load.
  • Treats the DOM as part of JavaScript The DOM is a browser API with its own standard, and JavaScript is the language that calls it. Node.js has no document object by default.
  • Mixes it up with React's virtual DOM The virtual DOM is a library's in-memory copy used to work out what changed. The real DOM is the browser's tree. Answer about the real one unless asked.
  • Gives a one-line definition and stops 'A tree of objects' alone skips how it gets built. Walk through parsing, one node per element, attribute and text, the document root, and scripts changing it.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime