JavaScript WorkerLocation toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

WorkerLocation.toString() is the stringifier for a worker’s location. It returns a string containing the serialized worker script URL. Per MDN, it is a synonym for WorkerLocation.href.

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

Syntax

Call location.toString. Start with this line.

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

Built into Web Workers. Call location.toString() inside a worker — no import required.

Library declarations
JavaScript
location.toString()

Parameters

ParameterDescription
Worker contextRequired. self.location is a WorkerLocation

Return Value

Return ValueDescription
*Serialized URL string (same as href)

JavaScript Examples

Example 1 — Call location.toString()

Post the serialized worker URL string to the main thread (MDN idea).

JavaScript
// Inside a Web Worker
const result = self.location.toString();
self.postMessage(result);
Try It Yourself

How It Works

  1. Worker context. Inside the worker, self.location is a WorkerLocation.
  2. Call the stringifier. toString() takes no arguments and returns the full URL string.
  3. Send it out. postMessage(result) lets the main page display or log that string.
  4. Blob labs differ. Try-it demos use blob workers, so you often see a blob:… URL instead of an https docs URL.

Example 2 — toString() vs href

Confirm both return the same serialized worker URL string.

JavaScript
// Inside a Web Worker
self.postMessage({
  viaToString: location.toString(),
  viaHref: location.href,
  same: location.toString() === location.href
});
Try It Yourself

How It Works

  1. Read both forms. Call toString() and read href on the same location.
  2. Compare with ===. Strict equality checks they are the exact same string.
  3. MDN synonym. When same is true, you have verified the documented synonym behavior.
  4. Pick either. Prefer href for a clear property name; use toString() when teaching stringifiers.

Example 3 — String Coercion

See how explicit toString() compares with String() and concatenation.

JavaScript
// Inside a Web Worker
self.postMessage({
  explicit: location.toString(),
  viaString: String(location),
  viaConcat: "" + location
});
Try It Yourself

How It Works

  1. Explicit call. location.toString() always returns the URL string clearly.
  2. String() asks the object for a primitive string and typically uses the stringifier.
  3. Concatenation ("" + location) also triggers string conversion.
  4. Teaching tip. Prefer an explicit toString() or href in beginner code so the intent is obvious.

Browser Support

WorkerLocation.toString() is marked Baseline Widely available on MDN (since July 2015). It is only available in Web Workers — not in Node.js, Bun, or Deno.

✓ Baseline · Widely available

WorkerLocation.toString()

Supported across modern browsers with Web Workers. Synonym for location.href inside a worker.

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
WorkerLocation.toString() Excellent

Bottom line: Browser / Web Workers only — not a Node.js API. Call location.toString() or location.href inside the worker—both serialize the worker script URL.

Frequently Asked Questions

A string with the serialized URL for the worker’s location—the same full URL string as WorkerLocation.href. Call it inside a worker as self.location.toString() or location.toString().
No. MDN marks WorkerLocation.toString() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is only available in Web Workers.
Yes. MDN documents toString() as a synonym for WorkerLocation.href—both give the serialized worker URL string.
No. Call location.toString() with no arguments.
When JavaScript needs a string—for example template literals, string concatenation, or String(location)—the stringifier can produce the same URL string.
WorkerLocation exists only in workers. On the page you use window.location.toString() / location.href instead—same idea, different object.

Next: href

Read the full serialized worker URL as a clear property name.

href property →

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