Edit Template
  • Home
  • /
  • JS Navigator Object
Edit Template
  • 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

FeatureCode ExampleDescription
Browser Detectionnavigator.userAgent.includes("Chrome")Detects browser type
Online Statusnavigator.onLineTrue/false based on internet connectivity
Geolocationnavigator.geolocation.getCurrentPosition()Fetches user’s location
Languagenavigator.languageUser’s preferred language
OS/Platformnavigator.platformReturns system/OS name
Scroll to Top