MutationObserver and Reacting to DOM Changes
A browser API for observing and reacting to changes in the DOM tree — added/removed nodes, attribute changes, text changes — asynchronously and efficiently.
MutationObserver is a browser API that lets you watch a specific part of the DOM tree and be notified asynchronously whenever it changes — nodes added or removed, attributes modified, or text content changed — without resorting to older, much less efficient techniques like polling the DOM repeatedly on a timer. It's a more advanced, less frequently used API day-to-day, but it comes up in interviews focused on browser internals, performance-sensitive tooling, or building things like rich text editors and browser extensions that need to react to arbitrary page changes they don't control directly.
MutationObserver is like a security camera with motion-triggered recording pointed at a specific room (the target node): instead of a guard manually walking back to check the room every few seconds (polling), the camera silently watches and delivers a batched summary of everything that happened the moment there's a natural pause to check the footage (microtask), rather than interrupting everything the instant a single item moves.