← Back to JavaScript Course | Chapter 11: Browser Object Model | Lesson 5 of 6

JS Navigator Object

window.navigator is like a passenger asking the driver questions about the vehicle itself, what browser is this, what language is it set to, are we currently online, rather than about the road (the page) being traveled.

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?

javascript
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

javascript
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

javascript
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

javascript
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

javascript
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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.