Frameworks hide the DOM, but interviews do not. This guide covers the eight browser questions that come up most — the DOM itself, event delegation, bubbling and capturing, innerHTML versus textContent, element sizes, async and defer, MutationObserver and the Shadow DOM.
Why interviewers ask about the DOM and events
Even in a React or Angular job, you will debug events, measure elements and fix scripts that load in the wrong order. Interviewers ask DOM questions to see whether you understand what the framework is doing for you — and what to do when it is not enough.
Security also comes up here: knowing why innerHTML with user input is dangerous is a small question with a big signal about how carefully you write code.
How to answer these questions
Tie every answer to something visible on the page: "with delegation, one listener on the list handles clicks on items added later" is clearer than a definition. Draw the event phases if you are at a whiteboard — down to the target, then back up.
Event handlers are closures, so the closures and functions guide explains why a handler might see a stale value.
Q1. What is the DOM?
The Document Object Model (DOM) is a tree-shaped, in-memory representation of an HTML document that JavaScript can read and change. Every element becomes a node in that tree.
Key APIs to know: document.querySelector(), document.createElement(), element.addEventListener(), element.setAttribute(), element.textContent and element.innerHTML.
Interview note: Follow-up: "why is touching the DOM in a loop slow?" Each change can trigger layout and repaint. Batch updates — build a
DocumentFragmentor a string first, then insert once.
Q2. What is event delegation?
Event delegation attaches one listener to a parent element and uses event bubbling to handle events from its children — including children added later.
document.getElementById("list").addEventListener("click", (e) => {
if (e.target.matches("li")) {
console.log("Clicked:", e.target.textContent);
}
});
// Works for <li> items added after this code runs
The benefits are fewer listeners, lower memory use and automatic handling of dynamic content.
Q3. What is the difference between event bubbling and event capturing?
An event travels in three phases: capture (from window down to the target), target, and bubble (back up to window). Listeners run in the bubble phase by default; pass true (or { capture: true }) to listen during capture.
// Default: bubble phase
el.addEventListener("click", handler);
// Capture phase
el.addEventListener("click", handler, true);
// Stop the event travelling further
el.addEventListener("click", (e) => {
e.stopPropagation();
});
Interview note: Follow-up: "
stopPropagationvspreventDefault?"stopPropagationstops the event moving to other elements;preventDefaultstops the browser's default action, such as following a link or submitting a form.
Q4. What is the difference between innerHTML and textContent?
innerHTML reads and writes HTML markup and parses tags — risky with user input because of XSS. textContent reads and writes plain text only, so tags appear as literal characters and it is safe for untrusted input.
const el = document.getElementById("box");
el.innerHTML = "<b>Bold</b>"; // renders bold text
el.textContent = "<b>Bold</b>"; // shows the literal string
Q5. What is the difference between offsetHeight, clientHeight and scrollHeight?
offsetHeight includes padding, border and scrollbar; clientHeight includes padding but not border or scrollbar; scrollHeight is the full scrollable content height, including what is hidden by overflow.
const el = document.getElementById("box");
console.log(el.offsetHeight); // e.g. 220
console.log(el.clientHeight); // e.g. 200
console.log(el.scrollHeight); // e.g. 500 if the content overflows
A common use: el.scrollTop + el.clientHeight >= el.scrollHeight tells you the user has scrolled to the bottom.
Q6. What is the difference between async and defer on a script tag?
Both download the script without blocking HTML parsing. async runs the script as soon as it downloads, in any order; defer waits until the HTML is parsed and runs scripts in document order, just before DOMContentLoaded.
<!-- blocks parsing while it downloads and runs -->
<script src="app.js"></script>
<!-- independent scripts, such as analytics -->
<script async src="analytics.js"></script>
<!-- scripts that need the DOM or each other -->
<script defer src="vendor.js"></script>
<script defer src="app.js"></script>
DOMContentLoaded fires when the HTML is parsed and deferred scripts have run; load fires later, once images and stylesheets have finished too. ES modules (type="module") are deferred by default.
Interview note: Follow-up: "where should a plain script tag go?" At the end of
<body>, or in<head>withdefer, so it does not block the page from rendering.
Q7. What is a MutationObserver?
MutationObserver watches a DOM tree for changes — added or removed nodes, attribute changes, text changes — and calls your callback asynchronously with a list of what changed.
const observer = new MutationObserver((mutations) => {
mutations.forEach((m) => console.log(m.type, m.target));
});
observer.observe(document.body, {
childList: true, // watch child nodes
subtree: true, // and all descendants
attributes: true, // watch attribute changes
});
// Later:
observer.disconnect();
Q8. What is the Shadow DOM?
The Shadow DOM encapsulates a DOM subtree inside a host element: styles and markup inside the shadow root do not leak out, and page styles do not leak in. It is the isolation layer behind Web Components.
const host = document.getElementById("my-widget");
const shadow = host.attachShadow({ mode: "open" });
shadow.innerHTML = `
<style>p { color: teal; }</style>
<p>Isolated content</p>
`;
// The <p> is scoped — external CSS cannot override it
How to prepare
Build a to-do list in plain JavaScript with no framework: add, delete and filter items using one delegated listener, and render text safely with textContent. It covers half of this guide in one small project.
Then read the browser APIs and performance guide for storage, lazy loading and avoiding expensive reflows.
Frequently Asked Questions
What is the difference between the DOM and HTML?
What is the virtual DOM?
What is the difference between event.target and event.currentTarget?
What is the difference between preventDefault and stopPropagation?
Which is faster: querySelector or getElementById?
Do I need DOM skills if I use React?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — View the Java Full Stack curriculum

