Question: **/!\ THE USE OF window.navigator.battery [IS STRONGLY DISCOURAGED](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/battery#Quick_Links) AND THIS ISSUE IS NOW NOT WORTH CHECKING THANK YOU /!\**

I want to get the battery level of the current system. Here is the output I intend to get:

**If desktop (no battery)**
Battery level : 100%

**If laptop**
Battery level : XXX% (depending the level)

So I tried the snippet of the Mozilla Foundation over [`window.navigator.battery`](https://developer.mozilla.org/en/docs/Web/API/Navigator/battery) in my **[JSFiddle](https://jsfiddle.net/)**.

**The problem :** Running Chrome 20+ version (currently 46), I still have an error on the console :

> 
> Uncaught TypeError: Cannot read property 'addEventListener' of undefined
> 
> 
> 

So what I understand is the object `battery` instantiated with the supposed battery level is returning nothing.

Did someone figured this out already ?

*The point of my algorithm when this snippet works is to define which action the user cannot perform regarding the remaining battery level. For example, I don't want the user to engage a heavy action that could probably lower his battery level to a critical point and make the critical action fail.*

Answer: 
**WARNING**

Do not use the following answer in production, as it has been [deprecated since then](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getBattery). The spec will be reworked due to [privacy and security issues](https://lukaszolejnik.com/AssessingPrivacyWebStandardsIWPE17.pdf), so expect to see changes and malfunctions.

**ANSWER**

To answer my own question, thanks to the help of the document that **@Quentin** provided to me, I reached my goal using `BatteryManager.level` function the the promise `Navigator.getBattery()`. The link that gives the working snippet can be found here : <https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager/level>.

The following JSFiddle display in the console (F12) the value of the remaining battery (I could not verify the ouput in a desktop yet, so please correct me if it is not working on desktop) :

**[JSFiddle](https://jsfiddle.net/1km5tsc4/)**

**JavaScript**

```
navigator.getBattery().then(function(battery) {

    var level = battery.level;

    console.log(level);
});

```
Navigator.getBattery() is now obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time.