Explain the role of the JavaScript task queue and microtask queue.
Instruction: Describe how the task queue and microtask queue function within the event loop.
Context: This question assesses the candidate's knowledge of the event loop's finer details and how different types of tasks are prioritized.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
In a browser, tasks include timer callbacks and dispatched event work. The event loop can have multiple task sources; it is not one universal FIFO for every kind of event. A microtask checkpoint drains queued microtasks, including any new microtasks they enqueue, before proceeding to a subsequent task.
Promise reactions, queueMicrotask callbacks and MutationObserver delivery use microtasks...