navigator: Checking Capabilities and Web APIsThe JavaScript window.navigator object exposes information about the browser environment and hardware capabilities, including network status, preferred language, clipboard access, and geolocation. Always test specific API support directly rather than guessing from navigator.userAgent.
navigator Properties and APIsnavigator.onLine: Returns true or false based on basic network connectivity (pair with "online" and "offline" window events).navigator.language / navigator.languages: Reports the user's preferred UI locale, such as "en-US".navigator.clipboard: Async Clipboard API (writeText(), readText()) for copy buttons over HTTPS.navigator.hardwareConcurrency: Approximate number of logical CPU cores available for sizing Web Worker pools.userAgent SniffingNever write if (navigator.userAgent.includes("Chrome")) to decide whether a feature works, because browsers regularly change user-agent strings or freeze them for privacy. Check the capability itself instead.
console.log("Online status:", navigator.onLine);
console.log("Preferred language:", navigator.language);
console.log("Logical CPU cores:", navigator.hardwareConcurrency ?? "unknown");
// Good: direct feature detection instead of userAgent parsing
async function copySnippet(text) {
if (!navigator.clipboard?.writeText) {
console.warn("Clipboard API unavailable in this context.");
return false;
}
await navigator.clipboard.writeText(text);
return true;
}
console.log("Clipboard API present:", Boolean(navigator.clipboard));
navigator.clipboard.writeText() on insecure http:// pages: Clipboard and Geolocation APIs require HTTPS (or localhost during development) and user interaction.navigator.onLine === true as proof of internet access: navigator.onLine only confirms a local network interface is connected; actual internet requests can still time out.navigator.platform or appVersion: Rely on CSS media queries for layout and feature checks ("geolocation" in navigator) for APIs.Call await navigator.clipboard.writeText(code) inside a button click handler wrapped in try...catch in case permissions are denied.
navigator.userAgent unreliable today?Modern browsers freeze or reduce userAgent details to protect user privacy, and many browsers include "Safari" or "Chrome" tokens inside each other's strings.
navigator.sendBeacon(url, data) do?sendBeacon() sends small analytics payloads asynchronously when a user closes or leaves a tab without delaying page navigation.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.