JavaScript Navigator userAgent Property

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline

What You’ll Learn

navigator.userAgent returns the browser’s User-Agent string. Learn how to read it, why sniffing browsers with it is unreliable, how UA reduction affects the string, five examples, and why feature detection is safer.

01

Kind

Read-only property

02

Returns

string (UA)

03

Baseline

Widely available

04

Sniffing?

Not recommended

05

Also via

HTTP User-Agent

06

Prefer

Feature detection

Introduction

Every browser has a long identification string called the User-Agent (UA) string. It usually starts with something like Mozilla/5.0 and then lists OS, engine, and browser tokens.

navigator.userAgent gives you that string in JavaScript. The browser also sends a similar value in the User-Agent HTTP header when it requests pages.

💡
MDN advice

Theoretically the UA string can help work around browser bugs — but sniffing is unreliable and not recommended. Prefer feature detection.

Modern Chromium browsers also reduce the UA string (frozen or simplified tokens) for privacy, so parsing brand and version from it gets even harder over time.

Understanding the userAgent Property

Think of navigator.userAgent as a name-tag string the browser already carries — useful for logging and learning, risky as the only way to decide which APIs to use.

  • Value — a single string (the UA string).
  • Shape — formal tokens (Mozilla/5.0, platform, AppleWebKit, Chrome, Firefox, Safari, …).
  • Related APIs — parts overlap with appVersion, platform, and Client Hints.
  • Reduction — many browsers freeze or shorten version / OS details.
  • Best practice — detect features, not brands.

📝 Syntax

General form of the property:

JavaScript
navigator.userAgent

Value

  • A string containing the current browser’s User-Agent.

Common patterns

JavaScript
console.log(navigator.userAgent);
// Chrome (reduced) example shape:
// "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36
//  (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36"

// Firefox example shape:
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0"

// Prefer feature detection over sniffing:
if ("serviceWorker" in navigator) {
  // safe to use service workers
}

⚡ Quick Reference

GoalCode
Read the UA stringnavigator.userAgent
Typetypeof navigator.userAgent // "string"
Lengthnavigator.userAgent.length
Substring check (demo only)navigator.userAgent.includes("Mozilla")
Identify browser?Unreliable — avoid for feature gates
Better approachFeature detection on APIs / CSS

🔍 At a Glance

Four facts to remember about navigator.userAgent.

Returns
string

User-Agent

Status
Baseline

Since Jul 2015

Sniffing
avoid

Unreliable

Prefer
detect

Feature first

📋 UA Sniffing vs Feature Detection

UA sniffingFeature detection
IdeaParse userAgent for “Chrome” / “Safari”Ask “does this API exist?”
ReliabilityLow — spoofable, reduced, shared tokensHigh for the capability you need
PrivacyUA reduction freezes detailsChecks only what you use
MDN stanceNot recommendedRecommended
Exampleua.includes("Chrome")"serviceWorker" in navigator

Examples Gallery

Examples follow MDN navigator.userAgent patterns. Prefer Try It Yourself — your live UA string will differ by browser and may be reduced.

📚 Getting Started

Read the UA string and inspect its type and length.

Example 1 — Read userAgent

Log the current User-Agent string (MDN’s basic example).

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

How It Works

MDN shows Chrome and Firefox sample shapes. Always expect browser-specific text.

Example 2 — Type and Length

Confirm beginners get a real string and see how long it is.

JavaScript
const ua = navigator.userAgent;
const lines = [
  "typeof: " + typeof ua,
  "length: " + ua.length,
  "starts with Mozilla/5.0?: " + ua.startsWith("Mozilla/5.0")
];
console.log(lines.join("\n"));
Try It Yourself

How It Works

Almost all desktop UA strings still begin with Mozilla/5.0 for historical compatibility.

📈 Practical Patterns

See why substring checks mislead, compare related fields, and prefer feature detection.

Example 3 — Why includes Sniffing Fails

Demo a classic beginner mistake: treating substring checks as brand detection.

JavaScript
const ua = navigator.userAgent;
const lines = [
  "includes(\"Mozilla\"): " + ua.includes("Mozilla"),
  "includes(\"Gecko\"): " + ua.includes("Gecko"),
  "includes(\"Chrome\"): " + ua.includes("Chrome"),
  "includes(\"Safari\"): " + ua.includes("Safari")
];
console.log(lines.join("\n"));
console.log("Many browsers share these tokens — do not gate features on them.");
Try It Yourself

How It Works

Chrome UA strings often contain both Chrome and Safari. Shared tokens make sniffing brittle.

Example 5 — Prefer Feature Detection

Keep userAgent for logging; use capability checks for features.

JavaScript
const lines = [];
lines.push(
  "userAgent length: " + navigator.userAgent.length + " — OK for logs"
);
lines.push(
  "serviceWorker: " +
    ("serviceWorker" in navigator ? "available" : "missing") +
    " — use this for features"
);
lines.push(
  "geolocation: " +
    ("geolocation" in navigator ? "available" : "missing")
);
console.log(lines.join("\n"));
Try It Yourself

How It Works

Matches MDN: feature detection is a much more reliable strategy than parsing the UA string.

🚀 Common Use Cases

  • Debugging / support logs — include the UA string when reporting bugs.
  • Learning HTTP — connect the JS property to the User-Agent request header.
  • Legacy code review — spot brittle includes("Safari") sniffing.
  • Not for feature gates — do not enable APIs based on brand tokens alone.
  • Analytics caution — reduced UAs make fine-grained OS / version stats less accurate.

🧠 How navigator.userAgent Works

1

Browser builds a UA string

Tokens describe engine, OS, and brand (sometimes reduced).

Build
2

Sent on HTTP requests

Servers see the User-Agent header; pages can read navigator.userAgent.

Header
3

Script reads the string

Useful for logs — weak as a capability detector.

Read
4

Apps detect features instead

Check APIs / CSS support rather than parsing brand names.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Read-only string on Navigator.
  • UA sniffing is unreliable; UA reduction makes it worse for version detection.
  • Related: userAgentData, appVersion, platform, product.

Universal Browser Support

navigator.userAgent is Baseline Widely available across browsers (MDN: since July 2015). Every modern browser exposes a UA string — but the exact text varies, and sniffing that text is not recommended.

Baseline · Widely available

Navigator.userAgent

Read the User-Agent string for learning and logging. Prefer feature detection for real application logic.

Universal Widely available
Google Chrome UA string (often reduced) · Desktop & Mobile
Full support
Mozilla Firefox UA string · Desktop & Mobile
Full support
Apple Safari UA string · macOS & iOS
Full support
Microsoft Edge UA string (Chromium / reduced) · Desktop
Full support
Opera UA string · Modern versions
Full support
Internet Explorer Legacy UA string support
Full support
userAgent Excellent

Bottom line: Log navigator.userAgent when debugging, but gate features with capability checks — not substring sniffing.

Conclusion

navigator.userAgent is the Baseline way to read the browser’s User-Agent string in JavaScript. Learn the shape of the string, remember that sniffing is unreliable (especially with UA reduction), and prefer feature detection for real apps.

Continue with userAgentData, platform, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use userAgent for logs and learning
  • Feature-detect the APIs you need
  • Expect reduced / frozen UA tokens
  • Document why legacy code parsed the string
  • Consider Client Hints on the server when needed

❌ Don’t

  • Gate features on includes("Safari") alone
  • Assume version numbers in the UA are exact
  • Treat shared tokens as unique brand IDs
  • Assign to navigator.userAgent
  • Ignore MDN’s sniffing warning

Key Takeaways

Knowledge Unlocked

Five things to remember about userAgent

Baseline UA string — great for logs, weak for sniffing, prefer feature detection.

5
Core concepts
🔒 02

Access

read-only

API
🚫 03

Sniffing

unreliable

Avoid
🛡 04

Privacy

UA reduction

Modern
🎯 05

Prefer

feature detect

Best

❓ 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 / 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 on the server when you truly need UA metadata.
Did you know?

Chrome’s UA string often contains both Chrome/… and Safari/… tokens. That historical compatibility is one reason includes("Safari") alone cannot identify Safari — and why MDN pushes feature detection instead of UA sniffing.

Learn userAgentData Next

Experimental User-Agent Client Hints via NavigatorUAData.

userAgentData →

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.

8 people found this page helpful