SmartCodingTips

🪟 The window & navigator Objects in JavaScript

The window and navigator objects are part of the Browser Object Model (BOM), providing access to the browser window and system-level information respectively.

🌐 The window Object

The window object is the global object in the browser environment. All global variables and functions are properties of this object.

// Accessing window properties
console.log(window.innerWidth);  // Width of the browser window
console.log(window.innerHeight); // Height of the browser window

// Using methods from window
alert("Hello from window!");
setTimeout(() => console.log("Executed after 2 seconds"), 2000);
  • window.alert() – Show alert popup
  • window.setTimeout() – Run after delay
  • window.location – Access or redirect URL
  • window.document – DOM document object

🔍 The navigator Object

The navigator object contains information about the user's browser and operating system.

// Basic browser info
console.log(navigator.userAgent);    // Full user agent string
console.log(navigator.language);     // Language (e.g., "en-US")
console.log(navigator.onLine);       // Check if user is online

// Check platform and cookies
console.log(navigator.platform);     // e.g., "Win32"
console.log(navigator.cookieEnabled); // true/false
  • navigator.userAgent – Browser info
  • navigator.language – Preferred language
  • navigator.platform – OS/platform
  • navigator.onLine – Connection status
  • navigator.geolocation – Get location (with permission)
💡 Tip: You can use navigator.geolocation.getCurrentPosition() to get the user's location (requires permission).

✅ Summary

  • window is the global browser object — everything (alert, setTimeout, location) lives here.
  • navigator gives you details about the browser and device (user agent, platform, etc.).