JavaScript Worker terminate() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

worker.terminate() immediately stops a dedicated Worker from the main thread (MDN). The worker does not get a chance to finish work or run cleanup — it is halted at once.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call worker.terminate. Start with this line.

JavaScript
worker.terminate()
CallWhat it does
worker.terminate()Primary signature.
worker.terminate(...)Call with the arguments your case needs.

Built into the browser. Call worker.terminate(...) on a Worker — no import required.

Library declarations
JavaScript
worker.terminate()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*undefined

JavaScript Examples

Example 1 — Terminate Immediately (MDN)

Create a worker, then stop it at once.

JavaScript
const code = `self.onmessage = () => {};`;
const url = URL.createObjectURL(
  new Blob([code], { type: "application/javascript" })
);
const myWorker = new Worker(url);

myWorker.terminate();
URL.revokeObjectURL(url);
console.log("Worker terminated");
Try It Yourself

How It Works

  1. Create a blob Worker. Same idea as MDN’s constructor + terminate.
  2. Call terminate(). The thread stops immediately.
  3. Revoke the blob URL. Lets the browser free that blob.
  4. Log confirmation on the main thread (worker cannot reply after halt).

Example 2 — Terminate After a Reply

Use the worker once, then hard-stop and revoke.

JavaScript
const code = `self.onmessage = (e) => {
  self.postMessage("pong: " + e.data);
};`;
const url = URL.createObjectURL(
  new Blob([code], { type: "application/javascript" })
);
const worker = new Worker(url);

worker.onmessage = (e) => {
  console.log(e.data);
  worker.terminate();
  URL.revokeObjectURL(url);
};

worker.postMessage("ping");
// pong: ping
Try It Yourself

How It Works

  1. Listen for onmessage before posting.
  2. Worker replies with pong: ping.
  3. Terminate in the handler after you have the result.
  4. Revoke the blob URL when the worker is no longer needed.

Example 3 — postMessage After Terminate

Do not expect a normal reply once the worker is gone.

JavaScript
const code = `self.onmessage = (e) => {
  self.postMessage("ok");
};`;
const url = URL.createObjectURL(
  new Blob([code], { type: "application/javascript" })
);
const worker = new Worker(url);

worker.onmessage = () => console.log("got reply");
worker.terminate();
worker.postMessage("too late");
console.log("no reply expected");
Try It Yourself

How It Works

  1. Terminate first. Worker is already stopped.
  2. postMessage after. No normal worker reply.
  3. onmessage stays silent for that late send.
  4. Treat the instance as gone after terminate.

Example 4 — Need Another Worker? Create Again

A terminated Worker object cannot be restarted.

JavaScript
function makeWorker() {
  const code = `self.onmessage = (e) => self.postMessage(e.data);`;
  const url = URL.createObjectURL(
    new Blob([code], { type: "application/javascript" })
  );
  return { worker: new Worker(url), url };
}

const first = makeWorker();
first.worker.terminate();
URL.revokeObjectURL(first.url);

const second = makeWorker();
second.worker.onmessage = (e) => console.log(e.data);
second.worker.postMessage("fresh");
// fresh
Try It Yourself

How It Works

  1. Terminate the first worker. That instance is finished.
  2. Revoke its blob URL.
  3. Create a second Worker. New thread, new object.
  4. Messaging works again on the new instance.

Example 5 — Graceful self.close() vs terminate()

MDN: workers can also stop themselves with close().

JavaScript
const code = `self.onmessage = (e) => {
  if (e.data === "done") {
    self.postMessage("closing");
    self.close(); // graceful stop from inside
    return;
  }
  self.postMessage("ok");
};`;
const url = URL.createObjectURL(
  new Blob([code], { type: "application/javascript" })
);
const worker = new Worker(url);

worker.onmessage = (e) => console.log(e.data);
worker.postMessage("done");
// closing
// (worker closed itself — no terminate() needed here)
Try It Yourself

How It Works

  1. Ask the worker to wrap up with a message.
  2. Worker posts a last reply, then self.close().
  3. Unlike terminate(), the worker chose when to stop.
  4. Use terminate() when main must cut the thread off immediately.

Browser Support

Worker.terminate() is Baseline Widely available (MDN: since July 2015). Available in Web Workers except Service Workers — not in Node.js, Bun, or Deno (use worker_threads there).

✓ Baseline · Widely available

Worker.terminate()

Immediately stop a dedicated worker from the main thread. No parameters. Safe everyday Web Worker API.

100% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
terminate Excellent

Bottom line: Safe for production dedicated workers. Prefer self.close() when the worker should finish cleanup first. See the MDN terminate reference.

Frequently Asked Questions

It immediately stops a dedicated Worker from the main thread. The worker does not get a chance to finish current work or run cleanup—it is halted at once.
No. MDN marks Worker.terminate() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is available in Web Workers except Service Workers.
No graceful shutdown from terminate(). If you need the worker to finish or clean up, send a message and let it call self.close() (or SharedWorkerGlobalScope.close() for shared workers) instead.
No. After terminate(), that Worker instance is done. Create a new Worker() if you need another background script.
Yes when you created the worker from URL.createObjectURL(blob). Call URL.revokeObjectURL(url) after terminate() so the blob can be garbage-collected.
postMessage after terminate will not get a normal worker reply. Treat the worker as gone and discard pending expectations.

Next: error

Learn how to detect when a dedicated worker script fails.

error event tutorial →

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.

9 people found this page helpful