The terminate() method of Workerimmediately stops the worker from the main thread. It does not let the worker finish or clean up. Learn when to use it, how it differs from self.close(), and how to free blob URLs—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
undefined
03
Params
None
04
Stops
Immediately
05
vs
self.close()
06
Status
Baseline widely
Fundamentals
Introduction
A dedicated worker started with new Worker() keeps running until you stop it or the page goes away. When the job is done—or you must cancel heavy work—call worker.terminate() from the main thread.
MDN is clear: the worker is stopped at once. There is no chance to flush files, send a final message, or tidy up. Prefer a cooperative “please stop” message plus self.close() inside the worker when you need a graceful exit.
💡
Beginner tip
Think of terminate() as the emergency stop button on the main thread. Think of self.close() as the worker choosing to shut itself down after finishing.
Concept
Understanding Worker.terminate()
An instance method that immediately terminates the dedicated Worker.
No parameters — call worker.terminate().
Return value — none (undefined).
Immediate — no opportunity for the worker to finish operations.
One-shot — that Worker object cannot be restarted; create a new one.
Baseline Widely available on MDN (since July 2015); available in Web Workers except Service Workers.
Foundation
📝 Syntax
JavaScript
terminate()
Parameters
None.
Return value
None (undefined).
Typical pattern
JavaScript
const myWorker = new Worker("worker.js");
// … use postMessage / onmessage …
myWorker.terminate();
Compare
⚖️ terminate() vs Related Ideas
Idea
Meaning
worker.terminate()
Main thread kills the worker immediately
self.close()
Worker stops itself (can finish current logic first)
postMessage
Talk to the worker while it is alive
URL.revokeObjectURL
Free a blob URL after terminate
new Worker()
Start a fresh worker after one was terminated
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Hard stop
worker.terminate()
Soft stop
Message the worker; it calls self.close()
Free blob
URL.revokeObjectURL(url) after terminate
Restart
new Worker(...) again
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about terminate().
Stops
now
No cleanup
Caller
main
Not the worker
Reuse
no
Create new Worker
Baseline
widely
Since Jul 2015
Hands-On
Examples Gallery
Examples follow MDN Worker: terminate(). Labs use blob workers so they run in one HTML file.
📚 Getting Started
Create a worker and stop it from the main thread.
Example 1 — Create Then Terminate (MDN Idea)
Start a worker and stop it right away.
JavaScript
const myWorker = new Worker(url);
myWorker.terminate();
console.log("Worker terminated");
Worker.terminate() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is available in Web Workers except Service Workers.
✓ Baseline · Widely available
Worker.terminate()
Immediately terminates a dedicated Worker from the main thread—no chance for the worker to finish.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported with workers in IE10+ (prefer modern browsers)
Legacy
Worker.terminate()Excellent
Bottom line: Use terminate() for hard cancels from main; prefer self.close() when the worker can exit cleanly after finishing.
Wrap Up
Conclusion
Worker.terminate() is the main-thread hard stop for a dedicated worker. Use it to cancel or tear down; use self.close() when the worker should finish gracefully. Always revoke blob URLs you created.
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.
Did you know?
MDN notes that dedicated and shared workers can also stop from inside via close() on their global scope. That path lets the worker finish sending a final result before exiting—something terminate() never allows.