JavaScript MutationObserver: Watching DOM Changes Quiz

JavaScript
0 Passed
0% acceptance

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.

35 Questions
~70 minutes
1

Question 1

What is the main advantage of MutationObserver over deprecated DOM mutation events?

A
It batches changes and delivers them asynchronously, improving performance
B
It can modify the DOM during observation
C
It works only with shadow DOM
D
It blocks rendering until mutations are processed
2

Question 2

How is a MutationObserver instantiated?

A
new MutationObserver(callback)
B
document.watchDOM(callback)
C
MutationObserver.create(callback)
D
window.MutationObserver(callback)
3

Question 3

When does the callback passed to MutationObserver execute?

A
Asynchronously, after all DOM mutations in the current task
B
Synchronously during each DOM change
C
Only on page load
D
Only when manually triggered
4

Question 4

What happens if you observe the same node multiple times with the same observer?

A
The observer uses the latest configuration
B
It throws an error
C
All configurations are merged
D
The first configuration remains active
5

Question 5

Which option enables observing direct child node additions or removals?

A
childList: true
B
subtree: true
C
attributes: true
D
characterData: true
6

Question 6

What does setting subtree: true allow you to observe?

A
Changes to all descendants of the target node
B
Only attribute changes on the target
C
Only text node changes
D
Changes outside the document
7

Question 7

How can you observe only specific attributes on a node?

A
Use attributeFilter: ["class", "id"] in the config
B
Set attributes: false
C
Use a regex in the observer
D
Manually check in the callback
8

Question 8

Which configuration option must be true to receive oldValue in a MutationRecord for attributes?

javascript
const config = { attributes: true, attributeOldValue: true };
A
attributeOldValue: true
B
oldValue: true
C
trackOld: true
D
history: true
9

Question 9

Which property of a MutationRecord holds the name of the changed attribute?

A
attributeName
B
attr
C
name
D
key
10

Question 10

What logs when a class attribute changes and oldValue is captured?

javascript
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'
A
'old-class'
B
'new-class'
C
null
D
undefined
11

Question 11

Can you observe changes to data-* attributes using MutationObserver?

A
Yes, by enabling attributes: true and optionally filtering
B
No, only standard HTML attributes are supported
C
Only if you use a special flag
D
Only in shadow DOM
12

Question 12

Which MutationRecord property contains nodes that were added to the DOM?

A
addedNodes
B
insertedNodes
C
newNodes
D
childNodes
13

Question 13

What type of object is addedNodes in a MutationRecord?

A
NodeList
B
Array
C
Set
D
DOMTokenList
14

Question 14

What happens if you append a node and then immediately remove it in the same tick?

A
It may appear in both addedNodes and removedNodes of the same record batch
B
The observer ignores it
C
It throws an error
D
Only the removal is recorded
15

Question 15

If subtree: true is set, will mutations on the target node itself be reported?

A
Yes, as long as the relevant options (e.g., childList, attributes) are enabled
B
No, only descendants are observed
C
Only if target is a Document node
D
Only with special permission
16

Question 16

What is a potential performance risk of using subtree: true?

A
It may generate a large number of mutation records on complex DOM trees
B
It blocks user input
C
It disables CSS animations
D
It prevents garbage collection
17

Question 17

Which configuration would observe text changes inside nested elements?

A
{ subtree: true, childList: true, characterData: true }
B
{ attributes: true }
C
{ childList: false }
D
{ subtree: false }
18

Question 18

What is the first argument passed to a MutationObserver callback?

A
An array of MutationRecord objects
B
A single MutationRecord
C
The target node
D
An event object
19

Question 19

Which property identifies the DOM node that was directly mutated?

A
target
B
node
C
element
D
source
20

Question 20

What does removedNodes contain when a parent node is removed?

A
Only the direct children of the target that were removed
B
All descendants of the removed node
C
Nothing, because subtree removal is not tracked
D
Only text nodes
21

Question 21

How can you determine if a mutation involved text content?

A
Check if type === "characterData"
B
Check if attributeName exists
C
Look for innerText in the record
D
It’s not possible
22

Question 22

Which method stops a MutationObserver from receiving further notifications?

A
disconnect()
B
stop()
C
unobserve()
D
pause()
23

Question 23

What happens to pending mutation records when disconnect() is called?

A
They are discarded and will never be delivered
B
They are delivered immediately
C
They remain queued until reconnect
D
They trigger an error
24

Question 24

Is it necessary to disconnect an observer when its target node is removed from the DOM?

A
Yes, to prevent memory leaks if the observer holds references
B
No, the browser automatically cleans it up
C
Only in older browsers
D
Only if using React
25

Question 25

Can you reconnect a disconnected MutationObserver?

A
Yes, by calling observe() again with a target and config
B
No, it becomes permanently inactive
C
Only if you recreate it
D
Only within 100ms
26

Question 26

Why is MutationObserver more efficient than polling the DOM?

A
It only runs when actual changes occur, avoiding constant checks
B
It runs in a web worker
C
It uses native C++ code
D
It disables layout recalculations
27

Question 27

What is a good practice to reduce observer overhead in a large application?

A
Observe the smallest possible subtree with precise config options
B
Use multiple observers on the same node
C
Always enable subtree: true
D
Avoid using attributeFilter
28

Question 28

How does MutationObserver affect layout thrashing?

A
It does not cause layout thrashing because it’s asynchronous and batched
B
It always forces a layout recalculation
C
It causes thrashing if used with getComputedStyle
D
It disables rendering until done
29

Question 29

What should you avoid doing inside a MutationObserver callback?

A
Making additional DOM mutations that trigger new observations
B
Logging to the console
C
Reading attribute values
D
Using setTimeout
30

Question 30

Which scenario is a typical use case for MutationObserver?

A
Detecting when a third-party widget (e.g., ad, chat) injects DOM elements
B
Replacing click event listeners
C
Measuring page load time
D
Encrypting user data
31

Question 31

How can MutationObserver help with dynamic UI state syncing?

A
By triggering state updates when DOM changes reflect user edits (e.g., in rich editors)
B
By preventing all DOM changes
C
By replacing React reconciliation
D
By serializing the DOM to JSON
32

Question 32

What is logged when a new element is appended to the observed container?

javascript
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'));
A
1
B
0
C
2
D
undefined
33

Question 33

Why might you use MutationObserver in an accessibility enhancement?

A
To dynamically update ARIA attributes or live regions when content changes
B
To disable screen readers
C
To reduce DOM size
D
To enforce color contrast
34

Question 34

What does this observer setup track?

javascript
observer.observe(node, {
  attributes: true,
  attributeFilter: ['disabled']
});
A
Changes only to the disabled attribute on the node
B
All attribute changes including class and id
C
Child node insertions
D
Text content updates
35

Question 35

In what scenario should you prefer a custom event over MutationObserver?

A
When you control the code that modifies the DOM and can dispatch events
B
When observing third-party DOM changes
C
When you need high performance with no trade-offs
D
When working with legacy browsers only

QUIZZES IN JavaScript