Skip to main content

How does window differ from document?

1. The main difference:

ObjectWhat it represents
windowthe entire browser window in which the page is loaded
documentthe HTML document itself (the window's inner content)

You could say:

window is "the entire browser window" document is "the page inside that window"

2. Relationship:

document sits inside window:

javascript
window.document === document; // true

That is:

javascript
window └── document (the page's DOM)

3. What belongs to each object:

window

Represents the tab's environment itself; contains everything related to the browser.

Examples:

javascript
window.innerWidth; // window width window.alert("Hi!"); // popup window window.location.href; // page URL window.localStorage; // storage

It also includes:

  • document (the page itself),
  • history (back/forward navigation),
  • navigator (browser info),
  • screen, console, setTimeout, fetch, and much more.

document

Represents the HTML page inside the window, that is, the DOM tree.

Examples:

javascript
document.title; // Page title document.body; // <body>...</body> document.querySelector("h1"); // Find an element document.createElement("p"); // Create an element

document is responsible only for the page's content: its structure, tags, text, styles, and so on.

4. An example combining both:

HTML:

javascript
<!DOCTYPE html> <html> <body> <h1 id="title">Hello</h1> <button id="btn">Click me</button> </body> </html>

JS:

javascript
// through document we work with the page const btn = document.getElementById("btn"); btn.addEventListener("click", () => alert("Click!")); // through window we work with the browser console.log(window.innerWidth); // window width console.log(window.location.href); // URL

5. Comparison by function

Capabilitywindowdocument
Controls the HTML pageIndirectlyYes
Controls the tab/browserYesNo
Contains the DOM treeYes (via document)Yes, is the DOM object itself
Methods for finding elementsNoYes
Methods for navigation and storageYesNo
Parent object-window
Global variablesStored here-

6. A real-life example:

  • window is the whole apartment (with walls, windows, and doors);
  • document is the room inside it (where the furniture is, and everything can be rearranged).

You can:

  • through document change the furniture (DOM elements),
  • through window open the door, look out the window, or turn on the light (actions related to the apartment itself, not the room).

In short:

Propertywindowdocument
RepresentsThe browser window (tab)The HTML document inside it
TypeGlobal environment objectDOM object
Where it's createdBy the browser when the tab opensAfter HTML parsing
Containsdocument, location, history, navigator, APIs, etc.DOM elements and the page's structure
Used forTimers, URL, events, storageManipulating HTML and the DOM
Examplewindow.innerWidthdocument.body

Summary: window is the entire browser window and its environment. document is the HTML page inside that window.

Through window you control the browser, through document you control the page's content.

Short Answer

Interview ready
Premium

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