JavaScriptMedium
What is the Navigator object?
window.navigator describes the browser and device the page is running on and is the entry point to many device-related APIs, such as clipboard, geolocation, online status, language, service workers and media devices.
Useful properties
navigator.onLine: whether the browser believes it has a network connection (listen toonline/offlineevents for changes).navigator.language/languages: the user's preferred languages, useful for i18n defaults.navigator.hardwareConcurrency: number of logical CPU cores, useful for sizing worker pools.navigator.userAgent: the user-agent string. It is unreliable and being reduced for privacy; prefer feature detection ornavigator.userAgentDatawhere supported.navigator.cookieEnabled,navigator.maxTouchPoints.
APIs hanging off navigator
navigator.clipboard: read/write the clipboard (requires a user gesture and HTTPS).navigator.geolocation: location, with a permission prompt.navigator.serviceWorker: register service workers for offline support and push.navigator.mediaDevices: camera and microphone (getUserMedia), screen capture.navigator.share: the native share sheet on supporting devices.navigator.sendBeacon(url, data): reliably send analytics when the page is closing.navigator.storage: storage estimates andpersist().navigator.permissions.query: check permission state without prompting.
Feature detection
Always check that an API exists before using it (if ("share" in navigator)), and handle rejections, since users can deny permissions.
Code example
js
1document.body.innerHTML = `<button id="copy">Copy coupon</button> <span id="msg"></span>`;23console.log({4 online: navigator.onLine,5 language: navigator.language,6 cores: navigator.hardwareConcurrency,7 touch: navigator.maxTouchPoints > 0,8});910// React to connectivity changes11window.addEventListener("offline", () => console.log("You are offline - changes will sync later"));12window.addEventListener("online", () => console.log("Back online"));1314// Clipboard (needs a user gesture and a secure context)15document.getElementById("copy").addEventListener("click", async () => {16 const msg = document.getElementById("msg");17 try {18 await navigator.clipboard.writeText("SAVE20");19 msg.textContent = "Copied!";20 } catch {21 msg.textContent = "Copy not allowed here";22 }23});2425// Native share sheet where available26if ("share" in navigator) {27 // navigator.share({ title: "DevQnA", url: location.href }) - call from a click handler28}2930// Storage quota estimate31try {32 const { usage, quota } = await navigator.storage.estimate();33 console.log(`using ${(usage / 1024).toFixed(1)} KB of ${(quota / 1024 / 1024).toFixed(0)} MB`);34} catch {35 console.log("storage estimate not available in this context"); // e.g. sandboxed iframes36}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.