Global object
Top-level browser APIs live on window.
window is the browser global object. It represents the tab and exposes many browser-level APIs.
console.log(window.innerWidth);
console.log(window.location.href);
window.addEventListener("resize", updateLayout);
Browser APIs & BOM
In a browser, global variables and many browser APIs live on window. The document, location, history, navigator and timers are all reachable from it.
You do not need to write window. for every global API, but knowing it exists helps you understand where browser features come from.
Be careful with global state. Adding your own values to window can create naming conflicts and hidden dependencies.
Top-level browser APIs live on window.
innerWidth and innerHeight describe the viewport.
resize, online, offline and visibility-related behavior can be observed.
Application state should not be scattered on window.
Examples
const state = { activePanel: "overview" };
export function getState() {
return state;
}
window.activePanel = "overview";
window.userSettings = {};
Code patterns
These examples focus on practical browser APIs: URL state, storage, cookies, clipboard, permissions, observers and offline-capable platform tools.
Useful for diagnostics, not a CSS replacement.
const width = window.innerWidth; const height = window.innerHeight;
React to viewport changes carefully.
window.addEventListener("resize", () => {
console.log(window.innerWidth);
});
Timers are window APIs in browsers.
const id = window.setTimeout(update, 500); window.clearTimeout(id);
Prefer modules over window state.
// Avoid: window.appState = {}
const appState = new Map();
Rules that matter
Browser APIs are powerful because they touch privacy, navigation, storage, permissions and performance. Use them deliberately.
It explains where many APIs live.
Global state creates conflicts.
Resize can fire many times quickly.
JavaScript viewport checks should not replace responsive CSS.
Window listeners can live for the page lifetime.
Not every window API exists everywhere.
Production thinking
Understanding window makes browser JavaScript less mysterious and helps avoid global-state mistakes.
Window-level events should not disrupt focus or keyboard interaction unexpectedly.
Production code should treat window as an environment boundary, not an application data store.
Client code that depends on window cannot run in every rendering environment, so critical content needs care.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain support, permissions, fallback and cleanup, you are ready to use the API responsibly.
Senior audit upgrade