JavaScriptDom And Eventsbeginner

JavaScript DOM and Events Interview Questions

The browser side of JavaScript interviews — the DOM, event delegation, bubbling and capturing, innerHTML vs textContent, element sizes, script loading, MutationObserver and the Shadow DOM.

Published
TL;DR – Quick Answer

DOM interviews check that you can work with the page without a framework: what the DOM is, event delegation, the capture and bubble phases, innerHTML versus textContent and the XSS risk, element size properties, async versus defer on scripts, MutationObserver and the Shadow DOM. Expect to write a small event handler during the interview.

On This Page

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 DocumentFragment or 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: "stopPropagation vs preventDefault?" stopPropagation stops the event moving to other elements; preventDefault stops 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> with defer, 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?
HTML is the text the server sends. The DOM is the live tree of objects the browser builds from that HTML, which JavaScript can read and change. Changing the DOM updates the page, but it does not change the original HTML file.
What is the virtual DOM?
A virtual DOM is a lightweight JavaScript copy of the UI used by libraries such as React. The library compares the new copy with the previous one and applies only the differences to the real DOM, which keeps updates efficient and the code declarative.
What is the difference between event.target and event.currentTarget?
event.target is the element that actually triggered the event, such as the clicked list item. event.currentTarget is the element whose listener is running right now, such as the list the listener was attached to. In delegation they are usually different.
What is the difference between preventDefault and stopPropagation?
preventDefault stops the browser's default action, such as following a link or submitting a form. stopPropagation stops the event from travelling to parent or child elements. They are independent, and sometimes you need both.
Which is faster: querySelector or getElementById?
getElementById is slightly faster because it does a direct lookup, but the difference rarely matters. Use querySelector for flexibility with CSS selectors, and cache the result in a variable instead of querying the DOM repeatedly inside loops.
Do I need DOM skills if I use React?
Yes. You will still handle events, read element sizes, manage focus, use refs and debug rendering. Understanding the real DOM makes framework behaviour — synthetic events, re-renders, effects — far easier to reason about.

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

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 28 November 2026 LinkedIn
Chat with us