Inside a page, every function the browser offers has to sit somewhere. setTimeout, alert and fetch do not come from the language itself: they belong to an object.
That object is window, and it is so central that writing its name can almost always be skipped.
Definition
window is the global object of a browser tab. It stands for the window displaying the page, and it carries both the browser interfaces and the global variables of the script. Any property of window can be used with no prefix at all.
window.setTimeout(() => console.log("one second"), 1000);
setTimeout(() => console.log("same thing"), 1000);
console.log(window.document === document); // trueBoth spellings point at the same function. The prefix only earns its place when an ambiguity has to be lifted, for instance when a local variable carries the same name.
What it carries
| Property | What it gives |
|---|---|
window.location | The address on display, and a way to navigate elsewhere |
window.innerWidth | The usable width of the window, in pixels |
window.history | The navigation history of the tab |
window.navigator | Details about the browser and the device |
window.localStorage | The persistent storage of the site, covered by localStorage |
On top of that come the events belonging to the window itself: resize, scroll, load, and beforeunload right before it closes.
Global variables and window
In a classic script, a var declaration at the top level creates a property on window. Neither let nor const does that, since they live in a separate scope.
var older = 1;
let modern = 2;
console.log(window.older); // 1
console.log(window.modern); // undefinedInside an ES module, loaded with type="module", even var stays local to the file. That is one of the reasons modules never pollute the global scope.
globalThis, the portable version
window only exists inside a page. Under Node.js, inside a Web Worker or in a test environment, referencing it throws a ReferenceError. globalThis names the global object wherever the code happens to run.
const inBrowser = typeof window !== "undefined";
globalThis.callCount = 0; // works everywhere
console.log(inBrowser);Frequently asked questions
Should window. be written before every call?
No, and everyday code does not do it. The prefix stays useful in two cases: when a local name shadows the global one, and when the existence of a recent interface is tested before use. Writing window.location rather than location also reads more clearly for a reviewer.
Are window.location and document.location the same?
Yes, both properties give the very same Location object, and a strict comparison confirms it. The window.location form is the common one, the form going through document is a leftover from the earliest versions of the language.
How can code tell it is running in a browser?
The test typeof window !== "undefined" is the safest one, because typeof never throws on an unknown name. That check is unavoidable in server-rendered code, where the same file runs once without any window and then once inside the browser.