← 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.
Syntax
javascript
navigator.userAgent;
navigator.language;
navigator.onLine;

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.

उदाहरण: What Is the Navigator Object?

javascript
// Print `navigator.userAgent` to the console
// Print `navigator.userAgent` to the console
console.log(navigator.userAgent);
// Print `navigator.language` to the console
// Print `navigator.language` to the console
console.log(navigator.language);
// Print `navigator.onLine` to the console
// Print `navigator.onLine` to the console
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.

उदाहरण: 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.

उदाहरण: 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.

उदाहरण: Online Status

javascript
// Print `navigator.onLine` to the console
// Print `navigator.onLine` to the console
console.log(navigator.onLine);
// Listen for the `offline` event on `window` and run the handler when it fires
// Listen for the `offline` event on `window` and run the handler when it fires
window.addEventListener("offline", () => console.log("Connection lost"));
// Listen for the `online` event on `window` and run the handler when it fires
// Listen for the `online` event on `window` and run the handler when it fires
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.

उदाहरण: Other Navigator Capabilities

javascript
console.log(typeof navigator.geolocation);
console.log(typeof navigator.clipboard); // both require user permission
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

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.