JavaScript Navigator serviceWorker Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

navigator.serviceWorker is a read-only property that returns the ServiceWorkerContainer for this document — the API entry point to register, update, remove, and talk to service workers (MDN).

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

Syntax

Use navigator.serviceWorker. Start with this line.

JavaScript
navigator.serviceWorker
CallWhat it does
navigator.serviceWorkerPrimary signature.

Built into the browser. Read navigator.serviceWorker on Navigator — no import required.

Library declarations
JavaScript
navigator.serviceWorker

Parameters

ParameterDescription
Secure contextRequired. HTTPS or localhost

Return Value

Return ValueDescription
*ServiceWorkerContainer

JavaScript Examples

Example 1 — Feature Detection

MDN’s beginner check before any register call.

JavaScript
if ("serviceWorker" in navigator) {
  console.log("Supported!");
} else {
  console.log("Service workers are not supported.");
}
Try It Yourself

How It Works

  1. Ask the browser. The in operator checks whether the property exists.
  2. Gate your code. Only call register() inside the supported branch.
  3. Private mode. Some browsers omit the API — this check catches that.
  4. No side effects. Detection does not install a worker.

Example 2 — Is the Page Controlled?

Read controller and listen for when a new worker takes over.

JavaScript
if (!("serviceWorker" in navigator)) {
  console.log("Service workers are not supported.");
} else if (navigator.serviceWorker.controller) {
  console.log("This page is currently controlled by a service worker.");
} else {
  console.log("No controlling service worker yet.");
}

if ("serviceWorker" in navigator) {
  navigator.serviceWorker.addEventListener("controllerchange", () => {
    console.log("Controller changed — a new worker took control.");
  });
}
Try It Yourself

How It Works

  1. Detect first. Skip the rest if the API is missing.
  2. Read controller. A ServiceWorker object means this page is controlled; null means not yet.
  3. First visit tip. After the first successful register, a reload is often needed before controller is set (MDN pattern).
  4. Listen for change. controllerchange fires when a new active worker takes control.

Example 3 — Register a Service Worker

Register a script at the site root (needs a real same-origin /sw.js).

JavaScript
if ("serviceWorker" in navigator) {
  navigator.serviceWorker
    .register("/sw.js")
    .then((registration) => {
      console.log("Service worker registration succeeded:", registration.scope);
    })
    .catch((error) => {
      console.error("Service worker registration failed: " + error);
    });
} else {
  console.log("Service workers are not supported.");
}
Try It Yourself

How It Works

  1. Gate with detect. Never call register without the in check.
  2. Pass a script URL. Default scope is based on that path (often site root for /sw.js).
  3. Handle both sides. Success gives a ServiceWorkerRegistration; failure rejects the Promise.
  4. Demo note. Without a same-origin worker file, failure is expected — host a minimal sw.js to see success.

Browser Support

Navigator.serviceWorker is part of the Service Worker API, marked Baseline Widely available on MDN (since April 2018). Requires a secure context.

✓ Baseline Widely available

Navigator.serviceWorker

Supported in Chrome, Firefox, Safari, Edge, and Opera. Entry point for register(), controller, and ready. May be missing in private mode.

Baseline Widely available
Google Chrome All modern versions
Yes
Microsoft Edge All modern versions
Yes
Mozilla Firefox All modern versions
Yes
Apple Safari All modern versions
Yes
Opera All modern versions
Yes
Internet Explorer Not supported
No
serviceWorker Excellent in browsers

Bottom line: Safe for modern PWAs and offline caching. See the MDN serviceWorker reference. Always feature-detect and serve over HTTPS.

Frequently Asked Questions

It is a read-only Navigator property that returns a ServiceWorkerContainer for the document — the entry point to register, update, remove, and communicate with service workers.
No. MDN marks it Baseline Widely available (since April 2018). It is not Deprecated, Experimental, or Non-standard.
Yes. It is available only in secure contexts (HTTPS or localhost) in supporting browsers.
Use MDN’s check: if ("serviceWorker" in navigator) { /* Supported! */ }
navigator.serviceWorker.controller is the ServiceWorker actively controlling this page, or null if none is controlling it yet.
MDN notes the feature may not be available in private/incognito browsing. Always feature-detect and keep a non–service-worker path.

Next: storage

Learn how navigator.storage estimates quota and requests persistent storage.

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

14 people found this page helpful