Web API Battery Last Updated : 28 Apr, 2025 Comments Improve Suggest changes 1 Likes Like Report Web API Battery is a JavaScript API that enables web applications to retrieve and track information about the device's battery status. This information includes the battery's charging status, level, and whether the device is currently charging or discharging. By utilizing this API, web developers can optimize their applications for energy efficiency and enhance the user experience. Concepts and UsageThe Web API Battery is straightforward to use. Developers can access the battery status by calling specific methods and listening to relevant events. Common use cases include: Adapting UI elements based on battery status.Implementing power-saving strategies.Alerting users when the battery level is critically low.InterfacesNavigator: The Navigator interface is a core element of web browsers, providing web developers with access to essential information about the user's browsing environment and facilitating interaction with web APIs.BatteryManager: Represents the battery manager interface.PropertiesBatteryManager.level: Returns the current battery level.BatteryManager.charging: Indicates whether the device is charging.BatteryManager.charging time: Returns the time until the battery is fully charged (in seconds).BatteryManager.dischargingTime: Returns the time until the battery is empty (in seconds).Web API Battery Methods:Navigator.getBattery(): Method to retrieve the BatteryManager interface.BatteryManager.requestBattery(): Requests battery information.Web API Battery Events:chargingchange: Fires when the device's charging status changes.chargingtimechange: Fires when the estimated time until full charge changes.dischargingtimechange: Fires when the estimated time until battery depletion changes.levelchange: Fires when the battery level changes.Example: In this example we are using every event and method. HTML <!DOCTYPE html> <html> <head> <title>Web API Battery Example</title> </head> <body> <h1>Battery Information</h1> <p id="battery-level">Battery Level: N/A</p> <p id="charging-status">Charging: N/A</p> <p id="charging-time">Charging Time: N/A</p> <p id="discharging-time">Discharging Time: N/A</p> <script> // Check if the Battery API is supported in // the current browser if ('getBattery' in navigator || 'battery' in navigator) { // Use navigator.getBattery() if available (newer browsers) const batteryPromise = navigator.getBattery ? navigator.getBattery() : navigator.battery; batteryPromise.then(function (battery) { // Display battery information updateBatteryInfo(battery); // Add event listeners for battery status changes battery.addEventListener('chargingchange', function () { updateBatteryInfo(battery); }); battery.addEventListener('chargingtimechange', function () { updateBatteryInfo(battery); }); battery.addEventListener('dischargingtimechange', function () { updateBatteryInfo(battery); }); battery.addEventListener('levelchange', function () { updateBatteryInfo(battery); }); }); } else { // Battery API is not supported alert('Battery API is not supported in this browser.'); } // Function to update battery information on the page function updateBatteryInfo(battery) { document.getElementById('battery-level') .textContent = `Battery Level: ${battery.level * 100}%`; document.getElementById('charging-status') .textContent = `Charging: ${battery.charging ? 'Yes' : 'No'}`; document.getElementById('charging-time') .textContent = `Charging Time: ${battery.chargingTime} seconds`; document.getElementById('discharging-time') .textContent = `Discharging Time: ${battery.dischargingTime} seconds`; } </script> </body> </html> Output: Browser SupportChromeMozilla Firefox Microsoft EdgeSafari Create Quiz Comment B buhari12mushraf Follow 1 Improve B buhari12mushraf Follow 1 Improve Article Tags : JavaScript Web Technologies Geeks Premier League Web-API Geeks Premier League 2023 +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like