What is the DOM in JavaScript?
The DOM, or Document Object Model, is the browser's object representation of an HTML document. It exposes the page as a tree of nodes that JavaScript can read and modify.
JavaScript interview questions covering the DOM, selectors, DOM updates, browser events, propagation, delegation, forms, the call stack and event loop.
Open a question for the full answer, code and interview guidance.
The DOM, or Document Object Model, is the browser's object representation of an HTML document. It exposes the page as a tree of nodes that JavaScript can read and modify.
getElementById searches for an element by its id and returns that element or null. querySelector accepts any valid CSS selector and returns the first matching element or null.
querySelector returns only the first element matching a CSS selector. querySelectorAll returns a static NodeList containing all matching elements.
Both represent collections of DOM nodes, but they are produced by different DOM APIs and can have different behavior. Many HTMLCollection objects are live, meaning they reflect relevant DOM changes automatically. A NodeList may be live or static depending on the API; querySelectorAll returns a static NodeList.
Use document.createElement to create an element, configure its content, attributes, classes, or other properties, and then insert it with methods such as append, appendChild, prepend, before, or after.
innerHTML reads or writes HTML markup inside an element, so assigned markup is parsed into DOM nodes. textContent reads or writes plain text and does not interpret HTML tags.
The classList property provides a convenient API for manipulating an element's CSS classes. Common methods include add, remove, toggle, contains, and replace.
getAttribute reads an attribute's string value, while setAttribute creates or updates an attribute. For custom data-* attributes, dataset provides a convenient object-style interface.
An event is a notification that something occurred in the browser or application environment. Examples include click, input, change, submit, keydown, focus, and load.
addEventListener registers a callback for a specified event type on an event target. Multiple listeners can be registered for the same event without overwriting one another.
When an event listener runs, the browser passes an event object containing information about the event. Common properties include type, target, currentTarget, and methods such as preventDefault and stopPropagation.
event.target is the element where the event originated, which may be a descendant of the element whose listener is currently running. event.currentTarget is the element whose event listener is currently being executed.
Event bubbling is the phase in which an event that originated on a descendant propagates upward through its ancestors. For many common events, listeners registered in the default mode run during this bubbling phase.
Capturing is the propagation phase that travels from the outer document hierarchy toward the event target before the target and bubbling phases. A listener can participate in capturing by passing { capture: true } or true to addEventListener.
preventDefault asks the browser not to perform the event's normal default action when that action is cancelable. For example, it can prevent a form submission or stop a link from navigating.
stopPropagation prevents an event from continuing through later ancestors in the capturing or bubbling propagation path. It does not automatically cancel the browser's default action.
Event delegation registers a listener on a common ancestor instead of adding a separate listener to every child element. The handler examines event.target, often with closest, to determine which relevant descendant triggered the event.
A listener attached directly to existing child elements does not automatically attach itself to new child elements added later. With delegation, the stable ancestor already has the listener, and bubbled events from new descendants reach it.
The input event generally fires whenever the value of an input-like control changes through user interaction, making it suitable for live validation or search. The change event generally fires when a value is committed, with exact timing depending on the control type.
Listen for the form's submit event rather than relying only on a button's click event. The submit event also covers keyboard submission and other valid ways a form can be submitted.
DOMContentLoaded fires after the HTML document has been parsed and the DOM is ready, without waiting for every image and other external resource to finish loading. The window load event fires after the page and its dependent resources have finished loading.
Synchronous code executes in sequence, and a long-running operation can prevent later work on the same thread from progressing. Asynchronous APIs allow work such as timers, network requests, or other external operations to complete later while JavaScript can continue processing other tasks.
The call stack tracks active execution contexts for functions currently being executed. When a function is called, a frame is pushed onto the stack; when it returns, that frame is removed.
The event loop coordinates execution between the JavaScript call stack and work scheduled by the runtime. When the call stack becomes available, queued work can be selected for execution according to the runtime's scheduling rules.
Microtasks include promise reaction callbacks and queueMicrotask callbacks. Tasks, often informally called macrotasks, include work such as timer callbacks and many browser events.