Example 1 — Read userAgent
Log the current User-Agent string (MDN’s basic example).
console.log(navigator.userAgent); How It Works
MDN shows Chrome and Firefox sample shapes. Always expect browser-specific text.

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.
Read-only property
string (UA)
Widely available
Not recommended
HTTP User-Agent
Feature detection
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.
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.
userAgent PropertyThink 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.
appVersion, platform, and Client Hints.General form of the property:
navigator.userAgent 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
} | Goal | Code |
|---|---|
| Read the UA string | navigator.userAgent |
| Type | typeof navigator.userAgent // "string" |
| Length | navigator.userAgent.length |
| Substring check (demo only) | navigator.userAgent.includes("Mozilla") |
| Identify browser? | Unreliable — avoid for feature gates |
| Better approach | Feature detection on APIs / CSS |
Four facts to remember about navigator.userAgent.
stringUser-Agent
BaselineSince Jul 2015
avoidUnreliable
detectFeature first
| UA sniffing | Feature detection | |
|---|---|---|
| Idea | Parse userAgent for “Chrome” / “Safari” | Ask “does this API exist?” |
| Reliability | Low — spoofable, reduced, shared tokens | High for the capability you need |
| Privacy | UA reduction freezes details | Checks only what you use |
| MDN stance | Not recommended | Recommended |
| Example | ua.includes("Chrome") | "serviceWorker" in navigator |
Examples follow MDN navigator.userAgent patterns. Prefer Try It Yourself — your live UA string will differ by browser and may be reduced.
Read the UA string and inspect its type and length.
userAgentLog the current User-Agent string (MDN’s basic example).
console.log(navigator.userAgent); MDN shows Chrome and Firefox sample shapes. Always expect browser-specific text.
Confirm beginners get a real string and see how long it is.
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")); Almost all desktop UA strings still begin with Mozilla/5.0 for historical compatibility.
See why substring checks mislead, compare related fields, and prefer feature detection.
includes Sniffing FailsDemo a classic beginner mistake: treating substring checks as brand detection.
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."); Chrome UA strings often contain both Chrome and Safari. Shared tokens make sniffing brittle.
MDN notes overlap with appVersion, platform, and related identification APIs.
const lines = [
"userAgent: " + navigator.userAgent,
"appVersion: " + navigator.appVersion,
"platform: " + navigator.platform
];
console.log(lines.join("\n"));
console.log("Still prefer feature detection for real app logic."); appVersion often looks like a slice of the UA string. platform is another legacy label and can be coarse or reduced.
Keep userAgent for logging; use capability checks for features.
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")); Matches MDN: feature detection is a much more reliable strategy than parsing the UA string.
includes("Safari") sniffing.navigator.userAgent WorksTokens describe engine, OS, and brand (sometimes reduced).
Servers see the User-Agent header; pages can read navigator.userAgent.
Useful for logs — weak as a capability detector.
Check APIs / CSS support rather than parsing brand names.
Navigator.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.
Read the User-Agent string for learning and logging. Prefer feature detection for real application logic.
Bottom line: Log navigator.userAgent when debugging, but gate features with capability checks — not substring sniffing.
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.
userAgent for logs and learningincludes("Safari") alonenavigator.userAgentuserAgentBaseline UA string — great for logs, weak for sniffing, prefer feature detection.
UA string
Valueread-only
APIunreliable
AvoidUA reduction
Modernfeature detect
BestChrome’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.
Experimental User-Agent Client Hints via NavigatorUAData.
8 people found this page helpful