Javascript platform detection

If you are building an app where you need to determine the platform on which your users are operating on (you have a download page with system-specific downloads or similar), you will most probably find this sexy function useful.

I wrote the function using some of the Ecmascript 6 syntax, which enabled me to write it as a one-liner.

const map = {
    Win: 'Windows',
    Win32: 'Windows',
    Mac: 'MacOS',
    X11: 'UNIX',
    Linux: 'Linux',
Object.keys(map).forEach(system => navigator.appVersion.indexOf(system) !== -1 ? localStorage.platform = map[system] : '');

The reason for the object and key value pairs is simple. The keys of the map object are the actual values we are looking for inside navigator.appVersion string, while the values are full names of the operating systems which you can then use to append to the view.

If you open your browser’s console and check the appVersion property on the navigator object


you will get a string which looks something like this:

"5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"

This is the actual string we do .indexOf() on (linear search) and look for name of the operating system.

That’s it for this one. How are you doing platform detection ? Is there something I can improve or even completely redo ?