How to Detect Operating System in JavaScript

Anika Tabassum Era Feb 02, 2024
  1. Detect Operating System With the platform Property in JavaScript
  2. Detect Operating System With the userAgent Property in JavaScript
  3. Detect Operating System With the appVersion Property in JavaScript
How to Detect Operating System in JavaScript

In JavaScript, the navigator object returns the browser information. Technically, through the navigator properties, we send information about the browser and sometimes information about the app or operating system we are using to the server.

As a result, the server returns a string containing information about the browser from where the ping was received earlier.

Here, we will see the use of the platform property with the navigator object in further instances. We will also see how the navigator.userAgent and navigator.appVersion works.

Detect Operating System With the platform Property in JavaScript

The platform property is an almost obliterated feature. Most browsers do not receive the exact details of the browsers’ platform.

Usually, the basic functionality of this property is to detect the platform from where the browser is running.

We will get the correct operating systems name when using this property, but the main problem is that some browsers like Chrome, Firefox 63, Edge returns the version as 32 even if the actual version is 64. So, even if it is the easiest way to figure out the operating system, yet ignores this entropy.

The following code lines will explain the case.

Code Snippet:

var OS = navigator.platform;
console.log(OS);

Output:

Detect Operating System with platform Property

As you can notice, we did receive the platforms’ name, but the real version is 64 of the PC from where the code is run. The browser we used is Chrome, and the returned message for version reflects 32.

Detect Operating System With the userAgent Property in JavaScript

If we want to track a little detail about the user’s used browser and platform, we can go for the userAgent property with the navigator object. This special feature returns a string containing the browser’s name, version, and platform name.

Unlike the previous property (platform), the userAgent gives a definite outcome, including the version.

In the following example, we will try to match a pattern from the string and infer it to its correct operating system.

Code Snippet:

var OS = 'Unknown';
if (navigator.userAgent.indexOf('Win') != -1) OS = 'Windows';
if (navigator.userAgent.indexOf('Mac') != -1) OS = 'MacOS';
if (navigator.userAgent.indexOf('X11') != -1) OS = 'UNIX';
if (navigator.userAgent.indexOf('Linux') != -1) OS = 'Linux';
console.log(OS);
console.log(navigator.userAgent);

Output:

Detect Operating System with userAgent Property

Detect Operating System With the appVersion Property in JavaScript

In the case of the appVersion property, we also retrieve the information about the browser and its platform. It is also recommended to know your PC’s operating system via JavaScript.

Yet, it serves a minimal purpose. You can have a sneak peek at this article for more info.

Also, this feature is a read-only property.

Code Snippet:

var OS = 'Unknown';
if (navigator.appVersion.indexOf('Win') != -1) OS = 'Windows';
if (navigator.appVersion.indexOf('Mac') != -1) OS = 'MacOS';
if (navigator.appVersion.indexOf('X11') != -1) OS = 'UNIX';
if (navigator.appVersion.indexOf('Linux') != -1) OS = 'Linux';
console.log(OS);

Output:

Detect Operating System with appVersion Property

Anika Tabassum Era avatar Anika Tabassum Era avatar

Era is an observer who loves cracking the ambiguos barriers. An AI enthusiast to help others with the drive and develop a stronger community.

LinkedIn Facebook