JavaScript Event Loop Essentials Quiz Quiz

Explore the basics of the JavaScript Event Loop, including the call stack, microtask and callback queues, and how asynchronous code is handled. Perfect for learners seeking a clearer understanding of JavaScript concurrency and async behavior.

  1. Call Stack Basics

    In JavaScript, what does the call stack do during code execution?

    1. It stores all variables used in your program.
    2. It executes one function at a time, following a last-in, first-out order.
    3. It manages only asynchronous code execution.
    4. It directly processes requests for web APIs.

    Explanation: The call stack is responsible for executing JavaScript functions one by one in a last-in, first-out way. It doesn't store program variables, which are managed separately in the execution context. It also does not specifically manage asynchronous code; that is coordinated using queues. The call stack doesn't interact directly with web APIs like timers or network requests.

  2. setTimeout and Queues

    When you use setTimeout(fn, 0) in JavaScript, where does the callback function get placed while it waits to run?

    1. Microtask queue
    2. Call stack
    3. Callback queue (task queue)
    4. Memory heap

    Explanation: setTimeout callbacks are placed in the callback queue, also known as the task queue, when their timer finishes. They do not go into the microtask queue, which is reserved for Promises and similar constructs. The call stack is only for immediate code execution, and memory heap is used for storing objects and variables, not callbacks.

  3. Role of the Event Loop

    What is the main role of the JavaScript Event Loop?

    1. It blocks all execution until timers finish.
    2. It checks if the call stack is empty, then processes queued tasks.
    3. It directly runs setTimeout callbacks only.
    4. It stores user inputs like clicks and keypresses.

    Explanation: The event loop continuously checks if the call stack is empty and then moves tasks from the queues (like microtasks and callbacks) into the stack for execution. It doesn't block other execution for timers or run setTimeouts directly. User inputs are separate and not directly managed by the event loop.

  4. Microtask vs Callback Queue

    Which JavaScript tasks are placed in the microtask queue?

    1. setTimeout and setInterval callbacks
    2. Promise.then and Promise.catch handlers
    3. console.log statements
    4. Synchronous function declarations

    Explanation: Promise.then and Promise.catch handlers are placed in the microtask queue, which is processed after the call stack becomes empty. setTimeout and setInterval callbacks go to the callback queue. console.log and function declarations are handled synchronously by the call stack.

  5. Order of Execution Example

    Given this code: console.log('A'); setTimeout(() => console.log('B'), 0); Promise.resolve().then(() => console.log('C')); console.log('D'); What is the order of console output?

    1. A D B C
    2. A B C D
    3. A D C B
    4. A C D B

    Explanation: The synchronous console.log('A') and console.log('D') run first. Promise.resolve().then places its callback in the microtask queue, which is executed after synchronous code. setTimeout's callback is placed in the callback queue, executed after microtasks. The other answer options don't follow the correct microtask and callback order.

  6. Web APIs Function

    What is the primary purpose of Web APIs in the JavaScript execution environment?

    1. To execute synchronous JavaScript code quickly
    2. To provide helpers that handle operations like timers and fetch requests in the background
    3. To add functions directly to the microtask queue
    4. To store all user data and application state

    Explanation: Web APIs handle asynchronous operations such as timers, AJAX/fetch calls, and DOM events outside of the JavaScript call stack. They do not execute synchronous code themselves nor do they write directly into the microtask queue. Web APIs also do not manage application data storage.

  7. setTimeout Timing

    Why does a setTimeout callback with 0 milliseconds delay not execute immediately after it is set?

    1. JavaScript does not support zero-delay timers.
    2. It waits until both microtasks and the call stack are clear before running.
    3. It executes before any synchronous code.
    4. It is blocked by the browser until user interaction occurs.

    Explanation: A setTimeout callback, even with 0ms delay, is queued in the callback queue and will only execute after all synchronous code and all microtasks are processed. JavaScript does support zero-delay timers, but they’re non-blocking and executed after microtasks. It is also not blocked by user interaction or executed before synchronous code.

  8. Synchronous vs Asynchronous

    Which of these statements about JavaScript execution is true?

    1. Synchronous code is handled after all queued tasks are executed.
    2. Asynchronous tasks like Promises are always handled before setTimeout callbacks.
    3. setTimeout callbacks are executed before Promise microtasks.
    4. Asynchronous code always blocks the call stack.

    Explanation: Promise microtasks are prioritized and executed before any timer callbacks from setTimeout. Synchronous code runs first before any tasks in the queues. setTimeout callbacks are handled after all microtasks, so that distractor is incorrect, and asynchronous tasks do not block the call stack.

  9. Only One Chef Analogy

    According to the restaurant analogy, why can JavaScript handle only one task at a time?

    1. It only works with one web browser at once.
    2. It is a single-threaded language by default.
    3. It can execute multiple functions simultaneously.
    4. It stores all code in a single memory heap.

    Explanation: JavaScript runs on a single-thread, meaning only one task (like the chef) is processed at any given moment. The analogy doesn’t relate to browsers or memory heaps, and JavaScript cannot execute multiple functions at the same exact time without extra features like web workers.

  10. Callback Queue Contents

    Which of the following is usually found in the callback queue?

    1. Results of Promise.then handlers
    2. setTimeout and setInterval callbacks
    3. Synchronous for loop iterations
    4. Immediate return values of functions

    Explanation: The callback queue handles tasks from setTimeout and setInterval, processed after the call stack and microtasks. Promise handlers are placed in the microtask queue, not the callback queue. Synchronous code and function return values are handled directly in the call stack.

  11. Promise Priority

    When a Promise is resolved, when will its .then() callback run?

    1. Immediately, before any remaining synchronous code
    2. After the current synchronous code, but before any setTimeout callbacks
    3. Only after all callback queue tasks run
    4. At the same time as the setTimeout callback

    Explanation: A Promise’s .then() callback is a microtask, so it runs after all synchronous code finishes but before any callback queue (e.g., setTimeout) tasks. It never interrupts ongoing synchronous code or waits until the callback queue is clear. It does not run concurrently with setTimeout.

  12. Analogy for Microtasks

    In the customer support center analogy, what do the VIP sticky notes represent?

    1. Emails from customers
    2. Timer-based callbacks
    3. Microtasks like Promise.then
    4. Calls from the manager

    Explanation: The VIP sticky notes in the analogy stand for microtasks such as Promise.then, which are addressed as soon as the current task is complete. Emails correspond to callback queue tasks, not microtasks. Calls from managers or other analogies are not specifically related to the microtask queue.

  13. Event Loop Cycle

    What happens when the call stack becomes empty in the event loop cycle?

    1. The event loop stops running entirely.
    2. Microtasks in the microtask queue execute first, followed by callback queue tasks.
    3. Only callback queue tasks are processed; microtasks are ignored.
    4. New synchronous code is immediately executed.

    Explanation: When the call stack is empty, all microtasks from the microtask queue are executed before any tasks in the callback queue. The event loop does not stop or ignore microtasks. New synchronous code is only executed if triggered from these tasks, not automatically.

  14. DOM Events and Queues

    In JavaScript, where are DOM event callbacks (like a click handler) placed before execution?

    1. Callback queue (task queue)
    2. Microtask queue
    3. Directly in the call stack
    4. Web API storage

    Explanation: DOM event callbacks are placed in the callback queue when the event occurs, waiting for the call stack to be free. They are not part of the microtask queue nor are they put directly into the call stack. Web API storage is not a term used for queuing such callbacks.

  15. Multiple Queues

    Why does JavaScript have separate queues for microtasks and callback (task) tasks?

    1. To run all callbacks in random order
    2. To prioritize and correctly schedule different tasks based on their importance
    3. To slow down execution for debugging
    4. To prevent synchronous code from running

    Explanation: JavaScript separates queues to ensure microtasks (like Promises) are executed before less urgent tasks (like timer callbacks), maintaining proper scheduling. Random execution or slowing down for debugging are not why the separation exists, and synchronous code still runs independently of these queues.

  16. Order of Promises and Timers

    If both a Promise.then and a setTimeout are scheduled at the same time, which will run first and why?

    1. setTimeout, because timers have higher priority
    2. Promise.then, because microtasks are always processed before callback queue tasks
    3. They will always run simultaneously
    4. The order is random every time

    Explanation: Promise.then is a microtask and will always execute before any pending setTimeout callbacks, which are processed in the callback queue after microtasks are cleared. Timers don’t have higher priority, tasks don’t run simultaneously, and the order is not random.