What does navigator do?
1. What navigator is
navigator is a property of the window object,
and it represents the Browser Object Model (BOM) - an interface
that lets JavaScript interact with the browser, not the page.
javascript
console.log(window.navigator === navigator); // true2. Example:
javascript
console.log(navigator.userAgent);
console.log(navigator.language);
console.log(navigator.onLine);Prints information like:
javascript
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)..."
"en-US"
true3. Main properties of navigator:
| Property | What it does |
|---|---|
navigator.userAgent | a string describing the browser and OS |
navigator.language | the user's interface language ("en-US", "uk-UA") |
navigator.languages | an array of preferred languages |
navigator.platform | the platform/OS ("Win32", "MacIntel", "Linux x86_64") |
navigator.onLine | true if there is internet access |
navigator.cookieEnabled | true if cookies are enabled in the browser |
navigator.hardwareConcurrency | the number of logical CPU cores |
navigator.deviceMemory | the device's RAM size (in GB) |
navigator.vendor | the browser vendor (e.g. "Google Inc.") |
4. navigator methods:
| Method | Description |
|---|---|
navigator.geolocation.getCurrentPosition() | get the user's geolocation |
navigator.clipboard.writeText() / readText() | work with the clipboard |
navigator.share() | trigger the system "Share" dialog (on mobile) |
navigator.vibrate() | vibrate on mobile devices |
navigator.mediaDevices.getUserMedia() | access the camera and microphone |
navigator.serviceWorker | work with Service Workers (PWA, offline mode) |
navigator.permissions.query() | check whether an API is allowed (e.g. geolocation) |
5. Usage examples:
Check the browser:
javascript
if (navigator.userAgent.includes("Chrome")) {
console.log("User is on Chrome");
}Check the connection:
javascript
if (!navigator.onLine) {
alert("No Internet connection!");
}Get the current coordinates:
javascript
navigator.geolocation.getCurrentPosition(pos => {
console.log(pos.coords.latitude, pos.coords.longitude);
});This method asks the user for permission.
Copy text to the clipboard:
javascript
navigator.clipboard.writeText("Hello, world!");Get the user's language:
javascript
console.log(`Your language: ${navigator.language}`);6. Structure:
javascript
window
└── navigator
├── userAgent
├── language
├── geolocation
├── clipboard
├── mediaDevices
└── ...In short:
| What it does | Provides information about the browser and device |
|---|---|
| Where it lives | Inside window (window.navigator) |
| What you can learn | browser type, language, OS, online status |
| What you can do | get geolocation, copy to clipboard, trigger share, use the camera |
| A typical example | navigator.userAgent, navigator.onLine, navigator.geolocation.getCurrentPosition() |
Summary:
navigatoris an object through which JavaScript can learn the browser's and device's characteristics and interact with system capabilities (geolocation, camera, clipboard, PWA, etc.).
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.