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 widthIn 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); // 10But - variables declared via let and const are not added to window:
javascript
let y = 5;
console.log(window.y); // undefined4. Main properties and objects inside window
| Property / Object | What it does |
|---|---|
window.document | The HTML document (DOM tree) |
window.location | URL information and navigation methods |
window.history | Navigation history |
window.navigator | Information about the browser and system |
window.localStorage | Persistent data storage |
window.sessionStorage | Temporary storage for the session |
window.console | Access to the console (console.log) |
window.alert(), confirm(), prompt() | Dialog windows |
window.setTimeout() / setInterval() | Timers |
window.innerWidth, window.innerHeight | Viewport dimensions |
window.scrollTo(), window.scrollBy() | Page scrolling |
5. Examples:
Navigation control:
javascript
console.log(window.location.href); // https://example.com
window.location.href = "https://example.org"; // navigate to another siteTimers:
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 more7. Key difference from document:
| Property | window | document |
|---|---|---|
| What it represents | The browser tab (window) | The page content (DOM) |
| Global object | Yes | No |
| Contains | The whole environment (DOM, timers, APIs) | Only the tree of HTML elements |
| Example | window.innerWidth | document.body |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.