JavaScript Web Workers Interview Questions and Answers
Understand how Web Workers move suitable computation off the main thread and exchange messages.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
Theoretical JavaScript Interview Questions
A Web Worker is a way to run JavaScript in a background thread, separate from the main browser thread. JavaScript is normally single-threaded, which means heavy computations can freeze the UI. A Web Worker lets you offload that heavy work to a background thread so the page stays responsive.
When JavaScript runs a heavy task (like sorting a huge dataset, processing images, or complex calculations), it blocks everything else including UI updates and user interactions because it is single-threaded. Web Workers solve this by running those tasks in a separate thread. The page stays smooth while the worker quietly does its job in the background.
You create a worker by passing a script file URL to the Worker constructor. The script runs in its own environment. You communicate with it using messages.
// main.js
const worker = new Worker("worker.js");
worker.postMessage({ task: "compute", data: [1, 2, 3] });
worker.onmessage = function (event) {
console.log("Result from worker:", event.data);
};
// worker.js
self.onmessage = function (event) {
const result = event.data.data.reduce((sum, n) => sum + n, 0);
self.postMessage(result);
};
Workers and the main thread communicate through message passing. The main thread sends data to the worker using worker.postMessage(data) and listens for responses via worker.onmessage. The worker does the same using self.postMessage() and self.onmessage. The data is copied (not shared) between threads by default, using the structured clone algorithm.
Web Workers run in an isolated environment with several restrictions:
- No access to the DOM,
window, ordocument - No access to
localStorageorsessionStorage - Communication with the main thread only happens through message passing
- The worker script must come from the same origin (or use CORS headers)
- Data sent in messages is copied, not shared by reference (unless you use Transferable Objects like
ArrayBuffer)
A dedicated worker is tied to one specific script or tab. Only that tab can talk to it. A shared worker (new SharedWorker()) can be accessed by multiple tabs or windows from the same origin at the same time. Shared workers use a port object for communication instead of direct message passing.
Both run in a background thread, but they serve very different purposes. A Web Worker is for CPU-heavy tasks for a specific page while that page is open. A Service Worker acts like a programmable network proxy. It can intercept network requests, cache responses, and even work offline. Service Workers have a lifecycle independent of any page and are the foundation of Progressive Web Apps (PWAs).
A Web Worker runs JavaScript in a background thread so CPU-heavy work can proceed without blocking the page main thread.
No. Workers do not have a window or DOM; they communicate with the page through messages and worker-supported APIs.
The page calls postMessage, and the worker can receive the data through its message event. Messages are structured-cloned or transferred where supported.
It copies supported data structures across the messaging boundary, while some transferable objects can move ownership. Functions and many host-specific values cannot be cloned.
Workers add startup, messaging, and data-copying overhead, so tiny tasks or frequent exchanges of large data may cost more than running on the main thread.
Register error handling, report a safe diagnostic, and provide a fallback or user-visible recovery path where possible; terminate workers that are no longer needed.
Ownership moves to the receiving context and the sender buffer becomes detached, avoiding a copy. Transfer only data the sender will no longer need in its current form.
No. A worker changes execution context but is not a general security sandbox; validate inputs, limit capabilities, and never rely on threading as an authorization boundary.