Question 18
Back to Exercise
The page contains this HTML:
<ul id="menu">
<li>Home</li>
<li>Courses</li>
</ul>What does the following JavaScript code print when it runs on this page?
const menu = document.getElementById("menu");
new MutationObserver((records) => console.log("observer:", records.length, "change(s)"))
.observe(menu, { childList: true });
setTimeout(() => console.log("timeout"), 0);
menu.append(document.createElement("li"));
menu.append(document.createElement("li"));
console.log("sync done");
- A observer: 1 change(s) observer: 1 change(s) sync done timeout
- B sync done observer: 2 change(s) timeout Correct
- C sync done timeout observer: 2 change(s)
- D sync done observer: 1 change(s) timeout
Correct Answer: Option B
Mutation records are batched and delivered in a microtask: after the synchronous code, before the timer task, with both changes in one call.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!