If you have ever logged a variable in the browser console and seen it pop up inside window, you have already met the global object that powers most browser APIs. New devs regularly ask how window relates to globals, DOM APIs, and environments like Node.js or Web Workers.
Hi folks,
I am trying to understand browser globals and keep seeing code that references window. What is the window object in JavaScript? How is it different from globalThis, how do I safely use it with frameworks or server-side rendering, and what are some common APIs or pitfalls I should know about?
In browsers, window is the global object that represents the current browser window or tab. It is the root for the Browser Object Model and hosts both the DOM entry point (document) and many web platform APIs.
- DOM access:
window.document,window.getSelection() - Navigation:
window.location,window.history - Storage:
window.localStorage,window.sessionStorage - Timing:
window.setTimeout,window.setInterval,window.requestAnimationFrame - Networking and other APIs:
window.fetch,window.navigator,window.matchMedia
- In traditional JavaScript, top-level
vardefines properties onwindow. For example,var foo = 1makeswindow.foo === 1. letandconstdo not attach towindow, which helps avoid accidental globals.globalThisis a standard global reference that works across environments. In browsers,globalThis === windowis true.
// Read the current URL
console.log(window.location.href);
// Update the title
window.document.title = "Hello from window";
// Listen for resize
window.addEventListener("resize", () => {
console.log("Viewport:", window.innerWidth, window.innerHeight);
});
// Schedule work on the next paint
window.requestAnimationFrame(() => {
// perform DOM reads/writes efficiently
});Code language: JavaScript (javascript)
Frameworks that support server-side rendering run your code in a non-browser environment where window does not exist. Always guard access:
const isBrowser = typeof window !== "undefined";
if (isBrowser) {
const theme = window.localStorage.getItem("theme");
console.log(theme);
}Code language: JavaScript (javascript)
Prefer globalThis if you write shared code. It resolves to the correct global object across browsers, Node.js, and Web Workers.
window.topgives you the topmost browsing context.window.parentreferences the parent frame.window.openerreferences the window that opened the current one (if allowed).
These are subject to same-origin restrictions. If windows are cross-origin, you cannot directly read their properties. Use postMessage for safe communication.
- Avoid polluting
windowwith app state. Use modules and closures instead. - Prefer feature detection over UA sniffing:
if ("matchMedia" in window) {
// safe to use matchMedia
}Code language: JavaScript (javascript)
- Debounce expensive handlers like scroll and resize.
The window object is ideal for adapting media delivery at runtime based on viewport, device capabilities, or network conditions. For example, you can switch image URLs depending on matchMedia or navigator.connection.effectiveType to deliver lighter assets.
// Example: choose an image variant on the client
const base = "https://res.cloudinary.com/demo/image/upload/";
const small = "c_fill,w_480,q_auto,f_auto/sample.jpg";
const large = "c_fill,w_1080,q_auto,f_auto/sample.jpg";
const prefersHighRes = window.matchMedia("(min-width: 900px)").matches;
document.querySelector("img.hero").src = base + (prefersHighRes ? large : small);Code language: JavaScript (javascript)
Cloudinary can transform and optimize media on the fly using URL parameters, so you can rely on a single original and let the client decide which to load.
- Never trust
window.locationinputs without validation. - Throttle
scrollandresizeevents. - Use passive listeners for scroll and touch when possible.
- Measure with Performance APIs and Lighthouse before and after changes.
In browsers, window is the global object hosting DOM access, navigation, storage, timing, and many other web APIs. Use globalThis for cross-environment code, guard window in SSR, and avoid attaching app state to it. You can leverage window features like matchMedia and navigator to adapt media at runtime, while Cloudinary handles on-the-fly transformations and format optimization to keep pages fast.
Ready to optimize and deliver media dynamically from the browser? Create your free Cloudinary account and start transforming images and video on the fly.