JavaScript WorkerLocation toString() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

The toString() stringifier method of a WorkerLocation object returns a string containing the serialized URL for the worker’s location. MDN documents it as a synonym for WorkerLocation.href. Learn when to call it, how it matches href, and how string coercion works—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

String

03

Params

None

04

Synonym

href

05

Context

Web Workers only

06

Status

Baseline widely

Introduction

Inside a worker, self.location is a WorkerLocation object that describes the worker script URL. Calling location.toString() turns that object into the full serialized URL string—the same value as location.href.

MDN’s classic idea: on a worker related to https://developer.mozilla.org/en-US/docs/Web, location.toString() can look like that full URL string.

💡
Beginner tip

Prefer location.href when you want a clear property name. Use toString() when you care about the stringifier / coercion behavior—both return the same URL text.

Understanding the toString() Method

An instance method (stringifier) on WorkerLocation that returns the serialized worker URL as a string.

  • Returns — a string (full worker URL).
  • Parameters — none.
  • Synonym — same result as location.href.
  • Accessself.location.toString() or location.toString() inside the worker.
  • Workers only — not available on the main document thread.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Call the method inside a worker script:

JavaScript
toString()

Parameters

None.

Return value

A string containing the serialized URL for the worker’s location (same as WorkerLocation.href).

Typical pattern (MDN idea)

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

⚡ Quick Reference

GoalCode / note
Call toStringself.location.toString()
Return typeString
Same asself.location.href
ParametersNone
Send to mainself.postMessage(location.toString())
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about WorkerLocation.toString().

Returns
URL string

Serialized

Where
in worker

self.location

Params
none

No arguments

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN WorkerLocation.toString(). Labs use blob workers and compare toString() with href.

📚 Getting Started

Call toString() the MDN way.

Example 1 — Call location.toString() (MDN Idea)

Post the serialized worker URL string to the main thread.

JavaScript
const result = self.location.toString();
// e.g. "https://developer.mozilla.org/en-US/docs/Web"
self.postMessage(result);
Try It Yourself

How It Works

The try-it lab posts the live blob worker toString() result plus the MDN sample string for comparison.

Example 2 — toString() vs href

Confirm they are the same serialized URL string.

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

How It Works

MDN’s synonym claim is easy to verify: both properties/methods yield the same string.

📈 Coercion & Blob URLs

See string coercion and blob worker URLs.

Example 3 — String Coercion

Template literals and String() can use the stringifier.

JavaScript
self.postMessage({
  explicit: location.toString(),
  viaString: String(location),
  viaTemplate: `${location}`
});
Try It Yourself

How It Works

Calling toString() explicitly is clearest for beginners; coercion often produces the same URL text.

Example 4 — Blob Worker URL

Log toString() with protocol for a blob worker.

JavaScript
self.postMessage({
  url: location.toString(),
  protocol: location.protocol
});
Try It Yourself

How It Works

Blob workers still serialize cleanly via toString()—you get the full blob:… URL string.

Example 5 — Worker vs Page URL String

Post the worker URL; compare it with location.toString() on the page.

JavaScript
// Worker
self.postMessage(location.toString());

// Main
worker.onmessage = (e) => {
  console.log("worker:", e.data);
  console.log("page:", location.toString());
};
Try It Yourself

How It Works

Same method name, different location objects: worker script URL vs page URL.

🚀 Common Use Cases

  • Log the full worker script URL for debugging.
  • Post the serialized URL to the main thread.
  • Rely on string coercion in templates or concatenation.
  • Teach that toString() and href match on WorkerLocation.
  • Inspect blob worker URLs without reading only host pieces.

🔧 How It Works

1

Worker URL

Main creates a Worker with an http(s) or blob script URL.

URL
2

WorkerLocation

Inside the worker, self.location describes that absolute script URL.

API
3

Call toString()

The stringifier returns the full serialized URL (same as href).

Call
4

Use or post

Log, compare with href, or postMessage the string to the main thread.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Only available in Web Workers (WorkerLocation / self.location).
  • Returns a string; takes no parameters.
  • Documented synonym for WorkerLocation.href.
  • Related learning: href, search, origin, Worker().

Universal Browser Support

WorkerLocation.toString() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is only available in Web Workers.

Baseline · Widely available

WorkerLocation.toString()

Returns the serialized worker URL as a string. Synonym for location.href inside a worker.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Limited / legacy workers path (prefer modern browsers)
Legacy
WorkerLocation.toString() Excellent

Bottom line: Call location.toString() inside the worker—or use location.href. Both serialize the worker script URL.

Conclusion

WorkerLocation.toString() is the stringifier for the worker script URL. It returns the same serialized string as href—call either one inside the worker when you need the full URL text.

Continue with href, search, origin, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Call toString() only inside a worker
  • Treat it as equal to location.href
  • Use postMessage to surface the URL on the page
  • Prefer explicit toString() or href over silent coercion in teaching code
  • Log blob URLs with the full serialized string

❌ Don’t

  • Expect WorkerLocation on the main thread
  • Pass arguments to toString()
  • Assume MDN’s https sample appears in blob demos
  • Confuse worker URL strings with the page URL
  • Use only host when you need the full URL (prefer href / toString())

Key Takeaways

Knowledge Unlocked

Five things to remember about WorkerLocation.toString()

The worker script URL, as a string.

5
Core concepts
⚙️02

Workers

self.location

Context
🔒03

No params

toString()

Rule
📄04

= href

same string

Synonym
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

A string containing 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.
Did you know?

Many DOM / URL objects expose a stringifier so "" + location or a template literal can print a useful URL. On WorkerLocation, that stringifier is toString()—and it matches href by design.

Explore more WorkerLocation

Continue with href, search, and Worker APIs.

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.

5 people found this page helpful