Skip to main content

What is window?

The window object is the global browser object, which represents the tab's window in which JavaScript runs.

It is the root object for the entire browser environment, and through it you can access all global variables, functions, and document itself.


1. What window is

window is an object that describes the current browser window and contains:

  • the web page itself (document),
  • window control methods (opening, closing, scrolling, and so on),
  • timers (setTimeout, setInterval),
  • browser APIs (localStorage, history, location, and others),
  • all global variables (for example, those declared via var).

2. Example:

javascript
console.log(window.document); // access to the DOM console.log(window.location.href); // page URL console.log(window.innerWidth); // browser window width

In practice, in the browser almost everything "lives" inside window.


3. window is a global object

When you write:

javascript
alert("Hello");

this is the same as:

javascript
window.alert("Hello");

Almost all global functions and properties actually belong to window. The same applies to variables declared via var:

javascript
var x = 10; console.log(window.x); // 10

But - variables declared via let and const are not added to window:

javascript
let y = 5; console.log(window.y); // undefined

4. Main properties and objects inside window

Property / ObjectWhat it does
window.documentThe HTML document (DOM tree)
window.locationURL information and navigation methods
window.historyNavigation history
window.navigatorInformation about the browser and system
window.localStoragePersistent data storage
window.sessionStorageTemporary storage for the session
window.consoleAccess to the console (console.log)
window.alert(), confirm(), prompt()Dialog windows
window.setTimeout() / setInterval()Timers
window.innerWidth, window.innerHeightViewport dimensions
window.scrollTo(), window.scrollBy()Page scrolling

5. Examples:

javascript
console.log(window.location.href); // https://example.com window.location.href = "https://example.org"; // navigate to another site

Timers:

javascript
window.setTimeout(() => { console.log("2 seconds have passed"); }, 2000);

Page scrolling:

javascript
window.scrollTo({ top: 0, behavior: "smooth" });

Storage:

javascript
window.localStorage.setItem("theme", "dark"); console.log(window.localStorage.getItem("theme")); // "dark"

6. Relationship:

javascript
window ├── document -> HTML document (DOM) ├── location -> URL and navigation ├── history -> navigation history ├── navigator -> browser information ├── localStorage / sessionStorage -> storage ├── console ├── timers (setTimeout, setInterval) └── ... and much more

7. Key difference from document:

Propertywindowdocument
What it representsThe browser tab (window)The page content (DOM)
Global objectYesNo
ContainsThe whole environment (DOM, timers, APIs)Only the tree of HTML elements
Examplewindow.innerWidthdocument.body

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.