How to find the operating system details using JavaScript?
How to find the operating system details using JavaScript?
239
13-Jul-2023
Updated on 14-Jul-2023
Aryan Kumar
14-Jul-2023Sure, here is how to find the OS details using JavaScript:
JavaScript
This code will first get the user agent string from the
navigatorobject. The user agent string is a string that identifies the browser and operating system that the user is using.Next, the code will use the
match()method to extract the OS name and version from the user agent string. Thematch()method takes a regular expression as an argument and returns an array of matches if the regular expression matches the string.The regular expression in this code matches the following strings:
Windows <version>Linux <version>Mac <version>Android <version>iOS <version>If the regular expression does not match the user agent string, the
match()method will return an empty array.Finally, the code will return the OS details if they were found. If the OS details were not found, the code will return
undefined.Here is an example of how to use this code:
JavaScript
This code will first call the
getOSDetails()function to get the OS details. If the OS details are found, the code will log the OS name and version to the console. If the OS details are not found, the code will log a message to the console.