JavaScript Worker() Constructor

Beginner
5 min read
Updated: Nov 2026
3 examples
Constructor

Definition

The Worker() constructor creates a dedicated Worker that runs a classic script or module at a given URL on a background thread (MDN). Use new Worker(url) or new Worker(url, options).

Pass the required arguments shown below. Always terminate unused workers; listen for error/messageerror as well as message.

Syntax

Create a Worker. Start with this line.

JavaScript
new Worker(url) / new Worker(url, options)
CallWhat it does
new Worker(url) / new Worker(url, options)Primary signature.

Built into the browser. Call new Worker(url, options) — no import required.

Library declarations
JavaScript
new Worker(url) / new Worker(url, options)

Parameters

ParameterDescription
urlRequired. Script / module the worker runs
options.typeOptional. "classic" (default) or "module"
options.nameOptional. Debug name → self.name

Return Value

Return ValueDescription
*A Worker instance

JavaScript Examples

Example 1 — Basic Blob Worker

Create a classic worker from a blob URL and echo a message.

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

worker.onmessage = (e) => {
  console.log(e.data); // "echo: hello"
  worker.terminate();
  URL.revokeObjectURL(url);
};

worker.postMessage("hello");
Try It Yourself

How It Works

  1. Build a script. Put worker code in a string for the demo.
  2. Make a blob URL. createObjectURL gives a same-origin blob: URL.
  3. Construct. new Worker(url) starts a dedicated classic worker.
  4. Message round-trip. Main sends "hello"; worker replies with echo: hello.

Example 2 — name Option

Pass { name } and read self.name inside the worker.

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

worker.onmessage = (e) => console.log(e.data); // "math-worker"
worker.postMessage(null);
Try It Yourself

How It Works

  1. Second argument. Pass an options object to new Worker(url, options).
  2. Set name. { name: "math-worker" } labels the worker scope.
  3. Read inside. The worker sees that label as self.name.
  4. Debug use. Names help identify workers in DevTools.

Example 3 — Off-Main-Thread Compute

Double a number in the worker so the main thread stays free.

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

worker.onmessage = (e) => console.log(e.data); // 42
worker.postMessage(21);
Try It Yourself

How It Works

  1. Start a worker. Same blob pattern as Example 1.
  2. Send input. Main posts 21 to the worker.
  3. Compute off-thread. Worker multiplies and posts 42 back.
  4. Why workers. Heavy work can stay off the UI thread.

Browser Support

Worker() is marked Baseline Widely available on MDN (since July 2015). Some options may have varying support. Available in Web Workers except Service Workers — not in Node.js, Bun, or Deno (use worker_threads there).

✓ Baseline · Widely available

Worker()

Create dedicated workers across modern browsers. Use same-origin scripts or blob: URLs for demos.

98% 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
Worker() Excellent

Bottom line: Safe for everyday dedicated workers. Check MDN if you rely on module workers, credentials, or Trusted Types.

Frequently Asked Questions

new Worker(url, options) creates a dedicated Worker that runs a classic script or module at the given URL on a background thread, separate from the main page UI thread.
No. MDN marks the Worker() constructor as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Some options may have varying support. It is available in Web Workers except Service Workers.
A same-origin script URL (resolved relative to the HTML page), or a blob: or data: URL. Cross-origin worker scripts usually need an intermediate same-origin worker or a blob loaded after a CORS fetch.
type: "classic" (default) runs a classic script and can use importScripts(). type: "module" runs an ES module with import, CORS fetching, and strict mode.
In a single HTML file you often have no separate worker.js. Building a Blob of JavaScript and passing URL.createObjectURL(blob) creates a same-origin worker URL you can start immediately. Prefer blob: over data: when possible.
Yes if the URL comes from untrusted input—the script is executed (an injection sink / XSS risk). Prefer fixed same-origin scripts, CSP worker-src, and TrustedScriptURL when enforcing Trusted Types.

Next: postMessage()

Send structured-clone data to your dedicated worker.

postMessage 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.

8 people found this page helpful