JavaScript Worker

Beginner
8 min read
Updated: Nov 2026
3 examples
Background threads

Definition and Usage

A Worker runs a JavaScript file on a background thread so heavy work does not freeze the page. You create one with new Worker("worker.js").

The page and the worker talk with postMessage and onmessage (or message listeners). Workers cannot touch the DOM. They are ideal for CPU-heavy loops, parsing, and compression.

This page teaches what Worker is. Constructor, methods, and events live under this section.

No DOM access inside a worker.

Pass data in and out with messages. Update the page only from the main thread after you receive a result.

JavaScript Syntax

Start a worker and exchange a message:

JavaScript
const worker = new Worker("worker.js");
worker.postMessage({ n: 40 });
worker.onmessage = function (event) {
  console.log(event.data);
};

Core Ideas

Ideas every Worker tutorial builds on:

IdeaExampleRemember
Createnew Worker("app.js")URL of the worker script
Sendworker.postMessage(data)Structured-clone copy
Receiveworker.onmessageResult arrives async
Inside workerself.onmessageListen on self
No DOMdocument …Unavailable in workers
Terminateworker.terminate()Stop from the page

JavaScript Examples

Three short programs that show how Worker works.

Example 1 — Construct a Worker

Pass the script URL to the constructor.

JavaScript
const worker = new Worker("worker.js");
console.log(typeof worker.postMessage);

How It Works

  1. new Worker("worker.js") starts the file.
  2. postMessage is ready on the instance.
  3. The browser loads the script asynchronously.

Example 2 — Round-trip a value

Page sends a number; worker replies with a result.

JavaScript
// page:
worker.postMessage(5);
// worker.js:
self.onmessage = (e) => self.postMessage(e.data * 2);

How It Works

  1. postMessage(5) sends to the worker.
  2. The worker multiplies and posts back.
  3. The page onmessage receives 10.

Example 3 — Stop a worker

terminate() ends the background thread immediately.

JavaScript
worker.terminate();
console.log("stopped");

How It Works

  1. terminate() kills the worker from the page.
  2. Pending messages are discarded.
  3. Create a new Worker if you need it again.

Next: Worker Guides

Continue with the constructor, postMessage, and worker events.

Look upWhy
Constructornew Worker(url, options)
postMessage()Send data to the worker
terminate()Stop the worker
message eventReceive replies on the page

Full lists: Worker constructor.

Frequently Asked Questions

A Worker runs a JavaScript file on a background thread. The page stays responsive while the worker computes, and they talk with postMessage.
No. Workers have no document. Send results back to the page and update the DOM there.
Call worker.terminate() from the page, or self.close() from inside the worker.
Most values are copied with the structured clone algorithm. Transferable objects can move ownership for big binary data.
Open the Worker constructor, then WorkerLocation for URL parts inside the worker.

Open the Worker constructor

Then practice postMessage and onmessage with a small worker file.

Worker constructor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful