JS Navigator Object
In this page:
What Is the Navigator Object?
window.navigator exposes information about the browser and the user's environment, such as which browser is running, what language it's configured for, and whether the device currently has a network connection.
Example: What Is the Navigator Object?
console.log(navigator.userAgent);
console.log(navigator.language);
console.log(navigator.onLine);
Reading the User Agent
navigator.userAgent returns a string describing the browser and OS, historically used for browser-sniffing logic, though modern code should prefer feature detection over parsing this fragile, frequently-inaccurate string.
Example: Reading the User Agent
console.log(navigator.userAgent); // prefer feature detection over parsing this
Language and Locale
navigator.language reports the user's preferred language as a BCP 47 tag like en-US, useful for serving localized content or formatting dates/numbers to match the visitor's locale preference.
Example: Language and Locale
console.log(navigator.language); // e.g. "en-US"
Online Status
navigator.onLine reports whether the browser currently believes it has network connectivity, and the online/offline events fire when that status changes, letting an app show a connectivity banner or queue actions for when the connection returns.
Example: Online Status
console.log(navigator.onLine);
window.addEventListener("offline", () => console.log("Connection lost"));
window.addEventListener("online", () => console.log("Back online"));
Other Navigator Capabilities
navigator is also the entry point for several browser APIs, including navigator.geolocation for location access (covered in more depth in the HTML course) and navigator.clipboard for reading/writing the system clipboard, both of which require explicit user permission.
Example: Other Navigator Capabilities
console.log(typeof navigator.geolocation);
console.log(typeof navigator.clipboard); // both require user permission
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: