JavaScript Navigator userAgent Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

navigator.userAgent is a read-only property that returns the browser’s User-Agent (UA) string — the same kind of value browsers send in the HTTP User-Agent header (MDN).

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use navigator.userAgent. Start with this line.

JavaScript
navigator.userAgent
CallWhat it does
navigator.userAgentPrimary signature.

Built into the browser. Read navigator.userAgent on Navigator — no import required.

Library declarations
JavaScript
navigator.userAgent

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*string

JavaScript Examples

Example 1 — Read the User-Agent String

Log the current browser’s UA string (MDN’s basic example).

JavaScript
console.log(navigator.userAgent);
Try It Yourself

How It Works

  1. Read the property. No arguments — it returns a string immediately.
  2. Content varies. Chrome, Firefox, Safari, and others format the string differently.
  3. Reduction. Modern browsers may freeze or shorten tokens for privacy (MDN).
  4. Use carefully. Fine for logging; poor as your only compatibility check.

Example 2 — Type and Length

Confirm you always get a string and inspect its size.

JavaScript
const ua = navigator.userAgent;

console.log(typeof ua);   // "string"
console.log(ua.length);   // character count
console.log(ua.slice(0, 40) + "…");
Try It Yourself

How It Works

  1. Store the value. Assign once if you will reuse it.
  2. Check typeof. Should be "string".
  3. Use length. Useful for logging / truncation in UIs.
  4. Slice for display. Long UA strings are fine to shorten in the UI.

Example 3 — Feature Detection Instead of Sniffing

MDN-aligned pattern: ask for the capability, not the browser name.

JavaScript
// Avoid: /Chrome/.test(navigator.userAgent)

if ("serviceWorker" in navigator) {
  console.log("Service workers supported");
} else {
  console.log("Service workers not supported");
}

if (navigator.clipboard && navigator.clipboard.writeText) {
  console.log("Clipboard write available");
} else {
  console.log("Clipboard write missing");
}
Try It Yourself

How It Works

  1. Skip name checks. Strings like "Chrome" appear in many browsers.
  2. Ask for the API. in / existence checks answer “can I use this?”
  3. Branch on capability. Enable features only when present.
  4. Keep a fallback. Progressive enhancement beats brittle UA regex.

Browser Support

Navigator.userAgent returns a string. MDN marks it Baseline Widely available (since July 2015). Not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Navigator.userAgent

Supported in all modern browsers and legacy ones. The string format and reduction behavior differ by browser — prefer feature detection for capabilities.

Baseline Widely available
Google Chrome All versions
Yes
Microsoft Edge All versions
Yes
Mozilla Firefox All versions
Yes
Apple Safari All versions
Yes
Opera All versions
Yes
Internet Explorer Supported
Yes
userAgent Universal in browsers

Bottom line: Safe to read everywhere. See the MDN userAgent reference. Do not use it as your primary feature gate.

Frequently Asked Questions

A string — the browser’s User-Agent (UA) string for the current browser. The same kind of value is also sent in the User-Agent HTTP header.
No. MDN marks it Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. MDN says UA sniffing is unreliable. Prefer feature detection. Modern browsers also reduce or freeze parts of the UA string for privacy.
No. It is a read-only Navigator property. You read the string; you do not assign a new value from page script.
The browser also sends a User-Agent header on HTTP requests. navigator.userAgent exposes a UA string to JavaScript in the page.
Check for the capability you need (for example "serviceWorker" in navigator), use progressive enhancement, and consider User-Agent Client Hints (navigator.userAgentData) when you truly need UA metadata.

Next: userAgentData

Learn how navigator.userAgentData exposes User-Agent Client Hints.

userAgentData tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful