Js Navigator in JavaScript
? JavaScript navigator Object
The navigator object contains information about the browser and operating system. It’s part of the Window interface and is often used for feature detection or identifying the user's environment.
? Basic Usage
console.log(navigator);It logs an object with properties like userAgent, language, platform, etc.
? Common navigator Properties
| Property | Description | Example Output |
|---|---|---|
navigator.userAgent | Info about the browser and OS | "Mozilla/5.0 (Windows NT 10.0; ...)" |
navigator.platform | The platform/OS | "Win32", "Linux x86_64" |
navigator.language | Preferred language of the browser | "en-US" |
navigator.onLine | true if the browser is online | true or false |
navigator.cookieEnabled | Whether cookies are enabled | true or false |
? Example: Detecting Browser Language
console.log("Your browser language is: " + navigator.language);? Example: Detect Online/Offline Status
if (navigator.onLine) { console.log("You're online!");} else { console.log("You're offline!");}You can also listen for status changes:
window.addEventListener('online', () => console.log('Back online!'));window.addEventListener('offline', () => console.log('You are offline.'));? Example: Check if Cookies Are Enabled
if (navigator.cookieEnabled) { console.log("Cookies are enabled.");} else { console.log("Cookies are disabled.");}?? Security Note
Some properties (like
userAgent) can be faked or restricted by privacy features.Never rely solely on
navigatordata for authentication or critical logic.
? Summary
The navigator object is useful for:
Browser/OS detection
Checking online status
Checking language preferences
Feature detection (e.g., if geolocation is available)
Would you like to see how to use navigator.geolocation to get user location?