JavaScript Navigator onLine Property

Beginner
5 min read
Updated: Nov 2026
5 examples
Property

Definition

navigator.onLine is a read-only Navigator property that returns a Boolean: true when the browser thinks the device is online, false when offline (MDN). When the value changes, window fires online or offline.

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

Syntax

Use navigator.onLine. Start with this line.

JavaScript
navigator.onLine
CallWhat it does
navigator.onLinePrimary signature.

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

Library declarations
JavaScript
navigator.onLine

Parameters

ParameterDescription
Request error handlingRequired. Truth about reachability

Return Value

Return ValueDescription
*The current value of navigator.onLine.

JavaScript Examples

Example 1 — Read onLine

Print the current Boolean status.

JavaScript
console.log(navigator.onLine);
Try It Yourself

How It Works

  1. Read the property. Returns a Boolean, not a string.
  2. true means “thinks online.” Not proof of Internet.
  3. false means “thinks offline.” Still verify with real requests.
  4. Read-only. You cannot assign a new value.

Example 2 — if / else (MDN)

Branch on the current status with MDN’s basic pattern.

JavaScript
if (navigator.onLine) {
  console.log("online");
} else {
  console.log("offline");
}
Try It Yourself

How It Works

  1. Truthy check. true takes the online branch.
  2. Else is offline. Simple label for demos and banners.
  3. Snapshot only. Status can change after this runs.
  4. Add listeners next. Example 3 keeps the UI in sync.

Example 3 — online / offline Events

Listen on window when the browser flips network status.

JavaScript
window.addEventListener("offline", () => {
  console.log("offline");
});

window.addEventListener("online", () => {
  console.log("online");
});

console.log("listeners ready; current: " + navigator.onLine);
Try It Yourself

How It Works

  1. Attach to window. MDN listens for both event names.
  2. Events fire on change. Not continuously while staying online.
  3. Update UI in the handler. Banner, badge, or retry queue.
  4. Test with airplane mode. DevTools Network offline also works in many browsers.

Example 4 — Soft UX Hint

Show a message without hard-disabling the rest of the app.

JavaScript
function networkHint() {
  if (navigator.onLine) {
    return "Connected (hint) — still handle request errors";
  }
  return "You may be offline — changes can sync later";
}

console.log(networkHint());
Try It Yourself

How It Works

  1. Return copy, not a lock. Soft guidance matches MDN advice.
  2. Online branch stays humble. Remind yourself to handle failures.
  3. Offline branch is hopeful. Queue / sync-later messaging.
  4. Pair with events. Re-run the helper inside listeners.

Example 5 — Status Helper

One small helper returns label + Boolean and wires listeners.

JavaScript
function watchNetwork(onChange) {
  function report() {
    const online = navigator.onLine;
    onChange({
      online: online,
      label: online ? "online" : "offline"
    });
  }
  window.addEventListener("online", report);
  window.addEventListener("offline", report);
  report();
  return function stop() {
    window.removeEventListener("online", report);
    window.removeEventListener("offline", report);
  };
}

watchNetwork((info) => {
  console.log(JSON.stringify(info));
});
Try It Yourself

How It Works

  1. Report immediately. First callback shows the current status.
  2. Share one handler. Both events call the same report.
  3. Return stop. Remove listeners when the UI unmounts.
  4. Keep apps resilient. Still treat failed requests as the real signal.

Browser Support

navigator.onLine is Baseline Widely available (MDN, since about July 2015). The online / offline window events are widely supported too.

✓ Baseline · Widely available

Navigator.onLine

Supported across modern Chrome, Firefox, Safari, Edge, and Opera. Heuristics differ by OS — treat the Boolean as a hint.

98% Property availability
Google Chrome onLine · online/offline events
Full support
Microsoft Edge onLine · Chromium
Full support
Apple Safari onLine · modern versions
Full support
Mozilla Firefox onLine · modern versions
Full support
Opera onLine · modern versions
Full support
Internet Explorer onLine · legacy support
Legacy support
navigator.onLine Excellent

Bottom line: Safe to read everywhere for UX hints. Pair with window events and real request error handling.

Frequently Asked Questions

A Boolean: true when the browser thinks the device is online, false when it thinks the device is offline (MDN).
No. MDN marks it Baseline Widely available (since about July 2015). It is not Deprecated, Experimental, or Non-standard.
Add window listeners for the online and offline events. They fire when navigator.onLine changes.
No. OS/browser heuristics vary. A LAN or virtual adapter may count as online without real Internet. Treat it as a UX hint, not proof a request will succeed.
MDN recommends against hard-disabling based only on this flag. Prefer soft hints and still handle fetch/XHR errors.
No. It is read-only. You read the Boolean and listen for events; you do not assign to it.

Next: oscpu

Learn how navigator.oscpu exposes a legacy OS/CPU string.

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

10 people found this page helpful