window in JavaScript: the browser global object

window is the global object of a tab: what it carries, its link with global variables, and why globalThis is preferred outside the browser.
3 min read
Believemy logo

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.

JAVASCRIPT
window.setTimeout(() => console.log("one second"), 1000);
setTimeout(() => console.log("same thing"), 1000);

console.log(window.document === document);   // true

Both 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

PropertyWhat it gives
window.locationThe address on display, and a way to navigate elsewhere
window.innerWidthThe usable width of the window, in pixels
window.historyThe navigation history of the tab
window.navigatorDetails about the browser and the device
window.localStorageThe 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.

JAVASCRIPT
var older = 1;
let modern = 2;

console.log(window.older);    // 1
console.log(window.modern);   // undefined
Good to know

Inside 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.

JAVASCRIPT
const inBrowser = typeof window !== "undefined";

globalThis.callCount = 0;   // works everywhere
console.log(inBrowser);


Frequently asked questions

Question

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.


Question

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.


Question

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.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.