JavaScript MutationObserver: Watching DOM Changes Quiz
Test your knowledge with 35 questions on MutationObserver—covering DOM observation, configuration options, attribute and child tracking, subtree monitoring, mutation records, cleanup, performance, and real-world use cases.
Question 1
What is the main advantage of MutationObserver over deprecated DOM mutation events?
Question 2
How is a MutationObserver instantiated?
Question 3
When does the callback passed to MutationObserver execute?
Question 4
What happens if you observe the same node multiple times with the same observer?
Question 5
Which option enables observing direct child node additions or removals?
Question 6
What does setting subtree: true allow you to observe?
Question 7
How can you observe only specific attributes on a node?
Question 8
Which configuration option must be true to receive oldValue in a MutationRecord for attributes?
const config = { attributes: true, attributeOldValue: true };Question 9
Which property of a MutationRecord holds the name of the changed attribute?
Question 10
What logs when a class attribute changes and oldValue is captured?
const observer = new MutationObserver(records => {
console.log(records[0].oldValue);
});
observer.observe(document.body, {
attributes: true,
attributeOldValue: true,
attributeFilter: ['class']
});
document.body.className = 'new-class'; // previously 'old-class'Question 11
Can you observe changes to data-* attributes using MutationObserver?
Question 12
Which MutationRecord property contains nodes that were added to the DOM?
Question 13
What type of object is addedNodes in a MutationRecord?
Question 14
What happens if you append a node and then immediately remove it in the same tick?
Question 15
If subtree: true is set, will mutations on the target node itself be reported?
Question 16
What is a potential performance risk of using subtree: true?
Question 17
Which configuration would observe text changes inside nested elements?
Question 18
What is the first argument passed to a MutationObserver callback?
Question 19
Which property identifies the DOM node that was directly mutated?
Question 20
What does removedNodes contain when a parent node is removed?
Question 21
How can you determine if a mutation involved text content?
Question 22
Which method stops a MutationObserver from receiving further notifications?
Question 23
What happens to pending mutation records when disconnect() is called?
Question 24
Is it necessary to disconnect an observer when its target node is removed from the DOM?
Question 25
Can you reconnect a disconnected MutationObserver?
Question 26
Why is MutationObserver more efficient than polling the DOM?
Question 27
What is a good practice to reduce observer overhead in a large application?
Question 28
How does MutationObserver affect layout thrashing?
Question 29
What should you avoid doing inside a MutationObserver callback?
Question 30
Which scenario is a typical use case for MutationObserver?
Question 31
How can MutationObserver help with dynamic UI state syncing?
Question 32
What is logged when a new element is appended to the observed container?
const target = document.getElementById('container');
const observer = new MutationObserver(records => {
console.log(records[0].addedNodes.length);
});
observer.observe(target, { childList: true });
target.appendChild(document.createElement('div'));Question 33
Why might you use MutationObserver in an accessibility enhancement?
Question 34
What does this observer setup track?
observer.observe(node, {
attributes: true,
attributeFilter: ['disabled']
});Question 35
In what scenario should you prefer a custom event over MutationObserver?
