Imagine you’re running a small restaurant. You’re the chef (that’s your JavaScript code). Customers keep coming in with orders, and you’re busy cooking. But one customer suddenly asks for a slow-cooked dish that takes 30 minutes.

If you, the chef, stay stuck cooking that dish, every other customer has to wait. Bad service!

Instead, you hire an assistant chef (a Worker). You hand them the slow task, while you keep taking other orders and serving customers. When your assistant is done, they bring the dish back to you.

That’s exactly what Web Workers do in JavaScript—they help you run heavy or time-consuming tasks in the background without freezing the main thread (the part of JavaScript that updates the webpage).


Why Do We Need Workers?

Normally, JavaScript runs on a single thread—one track. If you tell it to do something big, like processing a giant image or calculating millions of numbers, the page can “freeze” and stop responding.

Workers solve this by giving you background threads—extra helpers—so your app stays smooth and interactive.


Creating a Worker

Making a worker is like hiring that assistant.

// main.js
let worker = new Worker("worker.js");

Here, worker.js is the separate file where your assistant’s instructions live.


Talking to a Worker

You can send messages to your worker, and it can send messages back—like passing notes between the chef and assistant.

From the main script:

worker.postMessage("Start the task!");

Inside worker.js:

onmessage = function(event) {
  console.log("Worker received:", event.data);
  // Do the heavy task
  let result = event.data + " is done!";
  postMessage(result);
};

Back in main.js:

worker.onmessage = function(event) {
  console.log("Message from worker:", event.data);
};

A Real-Life Example: Image Processing

Suppose you’re building an app that applies filters to large images (like Instagram).

  • Without workers: the page freezes until the filter is applied.
  • With workers: the filter is applied in the background, while the user can still scroll, type, or click.
// main.js
worker.postMessage(hugeImageData);

// worker.js
onmessage = function(event) {
  let filtered = applyFilter(event.data);
  postMessage(filtered);
};

Limitations of Workers

  • Workers can’t touch the DOM (the actual HTML elements on your page). They only deal with data and logic.
  • They live in their own world (separate files or blob code).
  • Communication is only via postMessage and onmessage.

Types of Workers

  1. Dedicated Workers → For a single script (one-on-one helper).
  2. Shared Workers → Can be shared across multiple scripts.
  3. Service Workers → Special type that works for offline apps, caching, and background sync (used in Progressive Web Apps).

âś… Recap

  • Workers = background helpers in JavaScript.
  • Prevent the page from freezing during heavy tasks.
  • Communicate using postMessage and onmessage.
  • Perfect for big calculations, data processing, or image manipulation.

Review Fill-in-the-Gap Questions

  1. JavaScript normally runs on a single __________.
  2. Workers allow heavy tasks to run in the __________ without freezing the main page.
  3. A worker is created with the new __________("worker.js") syntax.
  4. To send data to a worker, you use the __________ method.
  5. A worker sends results back using the __________ method.
  6. Workers cannot directly access the __________ of a webpage.
  7. The communication between a script and a worker happens through __________ events.
  8. A worker dedicated to a single script is called a __________ worker.
  9. A special type of worker that handles offline caching is called a __________ worker.
  10. Workers make apps feel more __________ and __________.

<
Previous Post
🚀 JavaScript Arrow Functions: The Unusual Way of Writing Functions
>
Next Post
Stop Being Scared of AI: Should You Still Learn to Code?