JavaScript Navigator getBattery() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

navigator.getBattery() returns a Promise that resolves to a BatteryManager (MDN) — charging state, level (0–1), time estimates, and events such as chargingchange and levelchange.

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

Syntax

Call navigator.getBattery. Start with this line.

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

Built into the browser. Call getBattery(...) on a Navigator — no import required.

Library declarations
JavaScript
navigator.getBattery()

Parameters

ParameterDescription
Secure contextRequired. HTTPS / localhost

Return Value

Return ValueDescription
*Promise<BatteryManager>

JavaScript Examples

Example 1 — Feature Detection

Check getBattery and secure context before calling.

JavaScript
const lines = [
  "getBattery: " +
    (typeof navigator.getBattery === "function" ? "available" : "missing"),
  "isSecureContext: " + window.isSecureContext
];
console.log(lines.join("\n"));
Try It Yourself

How It Works

  1. Test the method. typeof … === "function" avoids calling a missing API.
  2. Check HTTPS. Chromium needs a secure context since Chrome 103.
  3. Expect missing. Firefox and some desktops simply omit the API.
  4. Keep the app usable. Battery UX is optional enhancement only.

Example 2 — Read Charging State (MDN)

MDN’s basic pattern: store battery.charging and update on change.

JavaScript
let batteryIsCharging = false;

if (!navigator.getBattery) {
  console.log("getBattery not supported");
} else {
  navigator.getBattery().then((battery) => {
    batteryIsCharging = battery.charging;
    console.log("charging: " + batteryIsCharging);

    battery.addEventListener("chargingchange", () => {
      batteryIsCharging = battery.charging;
      console.log("charging changed: " + batteryIsCharging);
    });
  }).catch((err) => {
    console.log("getBattery error: " + err.name);
  });
}
Try It Yourself

How It Works

  1. Guard first. Skip the call when getBattery is missing.
  2. Await the Promise. The resolved value is a BatteryManager.
  3. Read charging. Boolean for plugged-in / charging state.
  4. Listen + catch. Update on chargingchange; handle policy / security errors.

Example 3 — Battery Level Percent

Convert level (0–1) into a friendly percentage.

JavaScript
async function getLevelPercent() {
  if (!navigator.getBattery) return "getBattery not supported";
  try {
    const battery = await navigator.getBattery();
    return "level: " + Math.round(battery.level * 100) + "%";
  } catch (err) {
    return "Error: " + err.name;
  }
}

getLevelPercent().then(console.log);
Try It Yourself

How It Works

  1. Use async/await. Cleaner than nested .then for one read.
  2. Scale the level. level * 100 turns 0.87 into 87.
  3. Round for display. Math.round avoids noisy decimals.
  4. Catch errors. Soft-fail when the Promise rejects.

Example 4 — Snapshot + Listeners

Log a full snapshot and attach common BatteryManager events.

JavaScript
async function watchBattery() {
  if (!navigator.getBattery) return "getBattery not supported";
  const battery = await navigator.getBattery();
  const snapshot = () =>
    "charging=" + battery.charging +
    " level=" + Math.round(battery.level * 100) + "%";

  console.log("initial: " + snapshot());
  battery.addEventListener("chargingchange", () => {
    console.log("chargingchange: " + snapshot());
  });
  battery.addEventListener("levelchange", () => {
    console.log("levelchange: " + snapshot());
  });
  return "Listening for chargingchange + levelchange";
}

watchBattery().then(console.log);
Try It Yourself

How It Works

  1. Resolve once. Reuse the same BatteryManager for listeners.
  2. Print a snapshot. Charging + percent is enough for most demos.
  3. Subscribe to changes. Plug/unplug and level updates fire separately.
  4. Clean up in SPAs. Remove listeners when a component unmounts.

Example 5 — Low-Battery Soft Mode

Suggest reducing work when charge is low and not charging.

JavaScript
async function shouldUsePowerSaver(threshold = 0.2) {
  if (!navigator.getBattery) {
    return { powerSaver: false, reason: "unsupported" };
  }
  try {
    const battery = await navigator.getBattery();
    const low = battery.level <= threshold && !battery.charging;
    return {
      powerSaver: low,
      reason: low ? "low-battery" : "ok",
      levelPercent: Math.round(battery.level * 100),
      charging: battery.charging
    };
  } catch (err) {
    return { powerSaver: false, reason: err.name };
  }
}

shouldUsePowerSaver().then((result) => {
  console.log(JSON.stringify(result));
});
Try It Yourself

How It Works

  1. Default off. Unsupported → powerSaver: false (app keeps running).
  2. Combine level + charging. Low only matters when not plugged in.
  3. Return structured data. Easy to log or drive a soft UX mode.
  4. Never break core features. Treat this as an optional hint only.

Browser Support

navigator.getBattery() implements the Battery Status API and is not Baseline. Available in many Chromium builds on secure contexts; removed from Firefox for privacy; Safari support is limited or absent. Always feature-detect and handle Promise rejection.

✓ Limited availability · Not Baseline

Navigator.getBattery()

Promise → BatteryManager (charging, level, events).

Limited Not Baseline
Google Chrome Battery Status · secure context (103+)
Limited
Microsoft Edge Follow Chromium secure-context rules
Limited
Apple Safari Limited / often unavailable
Limited
Opera Follow Chromium Battery Status
Limited
Mozilla Firefox API removed for privacy
Unavailable
Internet Explorer No Battery Status / getBattery
Unavailable
getBattery() Limited

Bottom line: Detect getBattery, use HTTPS, catch errors, and keep the app fully usable when battery data is unavailable.

Frequently Asked Questions

It returns a Promise that resolves to a BatteryManager with charging state, battery level (0–1), charging/discharging times, and events such as chargingchange and levelchange.
No. MDN does not mark it Deprecated, Experimental, or Non-standard. It is Limited availability (not Baseline), requires a secure context in modern Chromium, and may be missing for privacy reasons in some browsers (notably Firefox).
Yes in supporting Chromium browsers since Chrome 103: getBattery() is a secure-context feature (HTTPS or localhost). Calling it from an insecure context can throw SecurityError.
A number from 0.0 to 1.0 representing remaining charge — for example 0.75 means about 75%.
No. Desktops without a battery, privacy-restricted browsers, and a blocked Permissions-Policy: battery can omit or reject it. Always feature-detect and handle Promise rejection.
No. Battery status is for helpful UX (pause heavy work on low battery), not fingerprinting. Prefer privacy-respecting, optional enhancements.

Next: getGamepads()

Learn how to read connected gamepad state from Navigator.

getGamepads() 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.

8 people found this page helpful