The difference between window and document
window is the whole browser window in which the page is loaded, while document is the HTML document itself, that is, the inner content of that window. document is nested inside window, so through the first object you control the browser and through the second the content of the page.
Theory
TL;DR
windowis the browser window (tab) and its entire environment.documentis the HTML page inside the window, that is, the DOM tree.documentis a property ofwindow:window.document === documentreturnstrue.windowgives you timers,location,history,navigator,screen,localStorage,fetchand dialogs.documentgives you searching for, creating and changing page elements.- Global variables are stored on
window, not ondocument.
Quick example
window.document === document; // true
console.log(window.innerWidth); // browser window width
console.log(window.location.href); // the page URL
console.log(document.title); // the page title
console.log(document.body); // the body elementThe main difference
| Object | What it represents |
|---|---|
window | the entire browser window in which the page is loaded |
document | the HTML document itself (the inner content of the window) |
You can put it like this:
windowis "the whole browser window".documentis "the page inside that window".
How they are related
document sits inside window:
window.document === document; // trueSo the structure is:
window
|-- document (the page DOM)What belongs to each object
window represents the environment of the tab itself and contains everything related to the browser:
window.innerWidth; // window width
window.alert("Hi!"); // popup dialog
window.location.href; // the page URL
window.localStorage; // storageIt also includes:
document(the page itself);history(going back and forward);navigator(information about the browser);screen,console,setTimeout,fetchand much more.
document represents the HTML page inside the window, that is, the DOM tree:
document.title; // the page title
document.body; // <body>...</body>
document.querySelector("h1"); // find an element
document.createElement("p"); // create an elementdocument is responsible only for the content of the page: its structure, tags, text, styles and so on.
A combined example
HTML:
<!DOCTYPE html>
<html>
<body>
<h1 id="title">Hello</h1>
<button id="btn">Click me</button>
</body>
</html>JavaScript:
// through document we work with the page
const btn = document.getElementById("btn");
btn.addEventListener("click", () => alert("Clicked!"));
// through window we work with the browser
console.log(window.innerWidth); // window width
console.log(window.location.href); // URLFeature by feature comparison
| Capability | window | document |
|---|---|---|
| Controls the HTML page | Indirectly | Yes |
| Controls the tab or browser | Yes | No |
| Contains the DOM tree | Yes, through document | It is the DOM object itself |
| Methods for finding elements | No | Yes |
| Methods for navigation and storage | Yes | No |
| Parent object | none | window |
| Global variables | Stored here | none |
An everyday analogy: window is the whole flat with its walls, windows and doors, while document is a room inside it, where the furniture stands and can be rearranged. Through document you move the furniture (the DOM elements), and through window you open the door, look out of the window or switch on the light, that is, do things that concern the flat rather than the room.
A short summary table:
| Property | window | document |
|---|---|---|
| Represents | The browser window (tab) | The HTML document inside |
| Type | The global environment object | A DOM object |
| When it is created | By the browser when the tab opens | After the HTML is parsed |
| Contains | document, location, history, navigator, APIs and more | DOM elements and the page structure |
| Used for | Timers, URLs, events, storage | Manipulating HTML and the DOM |
| Example | window.innerWidth | document.body |
So window is the whole browser window and its environment, while document is the HTML page inside that window. Through window you control the browser, through document the content of the page.
Common mistakes
- Looking for elements through
window: there are noquerySelectororgetElementByIdmethods there, they belong todocument. - Treating
documentas the global object. The global object iswindow, anddocumentis only one of its properties. - Accessing
document.bodybefore the HTML has been parsed. A script in<head>withoutdeferruns earlier, sodocument.bodywill benull. - Confusing
window.innerWidth(the viewport width) withscreen.width(the screen width) or withdocument.body.clientWidth. - Moving code to Node.js and expecting
windowordocumentto be there. Node.js has neither; the DOM exists only in the browser.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.