Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Battery API in JavaScript

Show battery percentage and charging state in JavaScript with navigator.getBattery and change events. Use HTTPS; many browsers do not expose battery data.

IntermediateJavaScript Browser APIsExample 14 of 15

Save this as index.html and serve it on HTTPS or localhost. Localhost is a web server on your own computer. Use the page buttons to request access.

battery-api.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<button id="battery">Read battery</button><p id="status"></p>
7<script>
8const status = document.querySelector('#status');
9document.querySelector('#battery').onclick = async () => {
10 if (!('getBattery' in navigator)) { status.textContent = 'Battery information is not supported.'; return; }
11 try {
12 const battery = await navigator.getBattery();
13 function render() {
14 status.textContent = 'Level: ' + Math.round(battery.level * 100) + '%, Charging: ' + battery.charging;
15 }
16 render();
17 battery.onlevelchange = render;
18 battery.onchargingchange = render;
19 } catch { status.textContent = 'Battery information is unavailable.'; }
20};
21</script>
22</body>
23</html>

Sample Output

Level: 85%, Charging: true

A sample level of 0.85 was multiplied by 100. Charging is a separate boolean, not a full-battery signal.

In this one

  • Check a limited-support API
  • Convert a fraction to a percentage
  • Read a charging boolean
  • Update when battery state changes

How it works

  1. 1

    Check getBattery before using it.

  2. 2

    Wait for the battery manager inside try/catch.

  3. 3

    Multiply level by 100 and round it for display.

  4. 4

    Install handlers for level and charging changes.

  5. 5

    Expect your own device values or the unsupported message, not a fixed 85 percent.

Edge cases

Missing API

Some widely used browsers have no getBattery method.

Show an unavailable state and keep the rest of the app usable. Do not assume every phone exposes battery data.

Unhelpful reading

A plugged-in desktop or restricted browser may show a constant value.

Treat this as optional information. It should not control access to lessons or make claims about the person.

What's going on

The Battery API reports battery level and charging state in browsers that expose it. An API is a browser feature your code can request. A practice page can use it to show a device-status message, while still working when the information is unavailable.

Use this browser-only example on HTTPS or localhost. getBattery returns a promise, which provides a BatteryManager after the browser finishes the request. Its level is usually a number from 0 to 1, so multiplying by 100 converts it to a percentage. charging is a boolean: true or false.

The page renders once and then updates when level or charging state changes. Assigning the two event properties replaces this example's previous handlers if the button is clicked again. Browser support is limited, partly because device data has privacy concerns. The displayed 85 percent is only an example, and some devices may report a default or uninformative value rather than a useful battery reading.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What percentage is level 0.4?

Pick an answer to check it.

Question 2

What should happen without getBattery?

Pick an answer to check it.

Questions people ask

Why multiply level by 100?

level is a fraction. A value of 0.85 becomes 85 percent.

Does charging true mean full?

No. It describes charging state, not whether the battery has reached 100 percent.

Can this read another device battery?

No. It only requests information exposed by the current browser on this device.