Explore key concepts and features of asynchronous programming in…
Start QuizExplore underrated JavaScript libraries that offer powerful solutions for…
Start QuizExplore how pnpm 10 enhances package installation security and…
Start QuizExplore how JavaScript classes relate to prototypes, constructor functions,…
Start QuizDiscover the proven patterns and practical habits that distinguish…
Start QuizAssess your understanding of JavaScript modules, including import/export syntax,…
Start QuizChallenge your understanding of JavaScript function definitions, syntax, and…
Start QuizSharpen your understanding of JavaScript objects with these easy…
Start QuizExplore the basics of JavaScript loops with these straightforward…
Start QuizExplore easy questions covering fundamental JavaScript concepts, suitable for…
Start QuizChallenge your JavaScript fundamentals with 15 essential interview questions…
Start QuizExplore essential concepts for handling dependencies across multi-package JavaScript…
Start QuizChallenge your understanding of fundamental JavaScript concepts with these…
Start QuizExplore the history and evolution of JavaScript, from its…
Start QuizEnhance your understanding of JavaScript with this beginner-friendly quiz…
Start QuizTest your understanding of common internet troubleshooting scenarios and…
Start QuizTest your knowledge of ES6 (ECMAScript 2015) features with…
Start QuizTest your understanding of key JavaScript topics with these…
Start QuizTest your knowledge with these commonly asked JavaScript interview…
Start QuizTest your knowledge of Observables in JavaScript, including their…
Start QuizTest your knowledge of essential JavaScript developer tips and…
Start QuizTest your knowledge of JavaScript ES6 features with this…
Start QuizTest your understanding of core JavaScript concepts relevant to…
Start QuizTest your understanding of Node.js fundamentals with this quiz…
Start QuizExplore 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.
This quiz contains 16 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.
In JavaScript, what does the call stack do during code execution?
Correct answer: It executes one function at a time, following a last-in, first-out order.
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.
When you use setTimeout(fn, 0) in JavaScript, where does the callback function get placed while it waits to run?
Correct answer: Callback queue (task queue)
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.
What is the main role of the JavaScript Event Loop?
Correct answer: It checks if the call stack is empty, then processes queued tasks.
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.
Which JavaScript tasks are placed in the microtask queue?
Correct answer: Promise.then and Promise.catch handlers
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.
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?
Correct answer: A D C 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.
What is the primary purpose of Web APIs in the JavaScript execution environment?
Correct answer: To provide helpers that handle operations like timers and fetch requests in the background
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.
Why does a setTimeout callback with 0 milliseconds delay not execute immediately after it is set?
Correct answer: It waits until both microtasks and the call stack are clear before running.
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.
Which of these statements about JavaScript execution is true?
Correct answer: Asynchronous tasks like Promises are always handled before setTimeout callbacks.
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.
According to the restaurant analogy, why can JavaScript handle only one task at a time?
Correct answer: It is a single-threaded language by default.
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.
Which of the following is usually found in the callback queue?
Correct answer: setTimeout and setInterval callbacks
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.
When a Promise is resolved, when will its .then() callback run?
Correct answer: After the current synchronous code, but before any setTimeout callbacks
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.
In the customer support center analogy, what do the VIP sticky notes represent?
Correct answer: Microtasks like Promise.then
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.
What happens when the call stack becomes empty in the event loop cycle?
Correct answer: Microtasks in the microtask queue execute first, followed by callback queue tasks.
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.
In JavaScript, where are DOM event callbacks (like a click handler) placed before execution?
Correct answer: Callback queue (task queue)
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.
Why does JavaScript have separate queues for microtasks and callback (task) tasks?
Correct answer: To prioritize and correctly schedule different tasks based on their importance
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.
If both a Promise.then and a setTimeout are scheduled at the same time, which will run first and why?
Correct answer: Promise.then, because microtasks are always processed before callback queue tasks
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.