JavaScript WorkerLocation port Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

WorkerLocation.port returns the port part of the worker’s location as a string. Per MDN, on http://localhost:8080/, location.port is "8080".

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use location.port. Start with this line.

JavaScript
location.port
CallWhat it does
location.portPrimary signature.

Built into Web Workers. Read self.location.port (or location.port) inside a worker — no import required.

Library declarations
JavaScript
location.port

Parameters

ParameterDescription
Worker contextRequired. self.location is a WorkerLocation

Return Value

Return ValueDescription
*String ("8080" or "")

JavaScript Examples

Example 1 — Read location.port

Post the worker port string (MDN idea: 8080).

JavaScript
// Inside a Web Worker on http://localhost:8080/
const result = self.location.port; // "8080"
self.postMessage(result);
Try It Yourself

How It Works

  1. Worker context. Inside the worker, self.location is a WorkerLocation.
  2. Read port. location.port returns the port part as a string.
  3. MDN shape. On http://localhost:8080/, the value is "8080".
  4. Blob labs differ. Try-it demos use blob workers, so the live port is often "".

Example 2 — port vs host

Compare port, host, and hostname on the same worker location.

JavaScript
self.postMessage({
  port: location.port,
  host: location.host,
  hostname: location.hostname
});
Try It Yourself

How It Works

  1. Three pieces. Read port, host, and hostname from the same location.
  2. port is the number. Just "8080" when a non-default port is shown.
  3. hostname is bare. No port in hostname.
  4. host combines. When shown, host looks like hostname:port.

Example 3 — Empty Port Check

Check whether port is an empty string (defaults / blobs).

JavaScript
self.postMessage({
  port: location.port,
  isEmpty: location.port === ""
});
Try It Yourself

How It Works

  1. Read port. Many blob workers and default-port URLs return "".
  2. Empty is a string. It is not null or undefined.
  3. Check clearly. Use port === "" before treating it as a number.
  4. Debug tip. Pair empty ports with href / protocol to explain why.

Browser Support

WorkerLocation.port 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.port

Supported across modern browsers with Web Workers. Read location.port inside the 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.port Excellent

Bottom line: Browser / Web Workers only — not a Node.js API. Expect MDN-style ports on non-default http(s) URLs; default ports and blob demos often print "".

Frequently Asked Questions

A string: the port part of the worker’s location. Access it inside a worker as self.location.port or location.port.
No. MDN marks WorkerLocation.port as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. It is only available in Web Workers.
host is hostname plus port when the port is shown. port is only the port number as a string (or an empty string when the default port is used).
For default ports (such as 80 for http or 443 for https), location.port is often the empty string "". Non-default ports like 8080 or 3002 usually appear as "8080" or "3002".
Blob workers often report an empty port. Always log port together with host, hostname, and href when debugging.
No. WorkerLocation.port is read-only.

Next: protocol

Read the protocol part of the worker script URL.

protocol 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