- Home
- /
- JS Navigator Object
JavaScript Tutorial
IINTRODUCTION
JAVASCRIPT VARIABLES
JAVASCRIPT BASICS
JAVASCRIPT OBJECTS
DOM & BOM
OOPs
- Home
- /
- JS Navigator Object
JavaScript Navigator Object
The navigator
object is part of the window
object in JavaScript. It provides information about the user’s browser and system. This helps you detect browser types, check internet connectivity, access geolocation, and more.
What is Navigator?
It’s a built-in object that helps your website adapt based on:
Browser type/version
Network connection
Operating system
Device capabilities
Geolocation support
Detect Browser
You can check what browser the user is using userAgent
:
if (navigator.userAgent.includes("Firefox")) {
console.log("User is on Firefox");
} else if (navigator.userAgent.includes("Chrome")) {
console.log("User is on Chrome");
} else {
console.log("Browser not identified");
}
userAgent
is a string describing the browser, OS, and more.
Check Online/Offline Status
You can detect if the user is connected to the internet using navigator.onLine
:
if (navigator.onLine) {
console.log("You are online.");
} else {
console.log("You are offline. Please check your connection.");
}
This is useful for offline-first web apps or showing alerts when connectivity is lost.
Get Geolocation
Using navigator.geolocation
, you can access the user’s physical location (with permission):
navigator.geolocation.getCurrentPosition(function(position) {
console.log("Latitude:", position.coords.latitude);
console.log("Longitude:", position.coords.longitude);
});
Geolocation requires user permission and works best on HTTPS.
Screen Dimensions
Though not part of navigator
, the screen
object is often used alongside it:
console.log(`Width: ${screen.width}px, Height: ${screen.height}px`);
You can use this to make layout decisions or alert users using very small devices.
Detect Language
Get the preferred browser language with navigator.language
:
console.log("Preferred language:", navigator.language);
// Output example: en-US
Check Platform
console.log("Operating System:", navigator.platform);
// Output: Win32, Linux x86_64, MacIntel, etc.
Conclusion
Feature | Code Example | Description |
---|---|---|
Browser Detection | navigator.userAgent.includes("Chrome") | Detects browser type |
Online Status | navigator.onLine | True/false based on internet connectivity |
Geolocation | navigator.geolocation.getCurrentPosition() | Fetches user’s location |
Language | navigator.language | User’s preferred language |
OS/Platform | navigator.platform | Returns system/OS name |