files/en-us/web/api/navigator/getbattery/index.md
{{ApiRef("Battery API")}}{{securecontext_header}}
The getBattery() method provides information about the system's battery.
It returns a battery promise, which resolves with a {{domxref("BatteryManager")}} object providing some properties to get the battery status also some events you can handle to monitor the battery status.
This implements the {{domxref("Battery Status API", "", "", "nocode")}}; see that documentation for additional details, a guide to using the API, and sample code.
Since Chrome 103, the Navigator.getBattery() method of {{domxref("Battery Status API", "", "", "nocode")}} only expose to secure context.
[!NOTE] Access to this feature may be controlled by the {{HTTPHeader("Permissions-Policy")}} directive {{HTTPHeader("Permissions-Policy/battery", "battery")}}.
getBattery()
None.
A {{JSxRef("Promise")}} that fulfills with a {{DOMxRef("BatteryManager")}} object which you can use to get information about the battery's state.
NotAllowedError {{domxref("DOMException")}}
SecurityError {{domxref("DOMException")}}
This example fetches the current charging state of the battery and establishes a handler for the {{domxref("BatteryManager/chargingchange_event", "chargingchange")}} event, so that the charging state is recorded whenever it changes.
let batteryIsCharging = false;
navigator.getBattery().then((battery) => {
batteryIsCharging = battery.charging;
battery.addEventListener("chargingchange", () => {
batteryIsCharging = battery.charging;
});
});
For more examples and details, see {{domxref("Battery Status API", "", "", "nocode")}}.
{{Specifications}}
{{Compat}}