Skip to content

JavaScript get user agent | Navigator userAgent

  • by

Use Navigator.userAgent read-only property to get user agent in JavaScript. This will return the user agent string for the current browser.

let agent = navigator.userAgent;

It returns a string representing values such as the name, version, and platform of the browser.

JavaScript user agent

Simple example code using navigator object.

<!DOCTYPE html>
<html>
<body>

  <script>
    let agent = navigator.userAgent;
    console.log("User agent", agent)
  </script>

</body>
</html>

Output:

JavaScript get user agent

Display all navigator properties:

<!DOCTYPE html>
<html>
<body>


  <div id="demo"></div>

  <script>
    let text = "<p>Browser CodeName: " + navigator.appCodeName + "</p>" +
    "<p>Browser Name: " + navigator.appName + "</p>" +
    "<p>Browser Version: " + navigator.appVersion + "</p>" +
    "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>" +
    "<p>Browser Language: " + navigator.language + "</p>" +
    "<p>Browser Online: " + navigator.onLine + "</p>" +
    "<p>Platform: " + navigator.platform + "</p>" +
    "<p>User-agent header: " + navigator.userAgent + "</p>";

    document.getElementById("demo").innerHTML = text;
  </script>

</body>
</html>

Output:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Cookies Enabled: true

Browser Language: en-US

Browser Online: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

You can parse this string to extract specific information about the browser, operating system, etc., if needed. However, keep in mind that user-agent strings can be spoofed or modified, so they may not always provide accurate information.

Do comment if you have any doubts or suggestions on this JS userAgent topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *