Course contentsShow
← JavaScript
Lesson 1468 of 2,72332. JavaScript in the BrowserFree lesson

Window and Document Objects

Understanding the global window object and how document provides access to the DOM tree in browsers.

Window and Document Objects

You'll learn how the global window object and the document object give you control over the browser environment and the page content.

The Window Object: Your Browser's Control Center

The window object is the global object in browsers—it represents the entire browser window or tab. Everything browser-related lives here: navigation controls, timing functions, screen dimensions, and even the page content itself.

Think of window as the building that houses your web application. It contains the walls (viewport), doors (navigation), and everything else about the physical space.

When you access global browser features like setTimeout, alert, or console, you're actually accessing window.setTimeout, window.alert, and window.console—the window. prefix is just optional because it's the global object.

console.log(window === globalThis); // true in browsers
console.log(window.innerWidth);     // viewport width in pixels
console.log(window.location.href);  // current URL

The Document Object: Your Page's Structure

The document object is a property of window that represents the HTML page itself. It provides access to the DOM (Document Object Model)—the tree structure of all HTML elements.

If window is the building, then document is the floor plan and furniture layout. It's where you find and manipulate the actual content users see.

console.log(window.document === document); // true
console.log(document.title);               // page title
console.log(document.body);                // <body> element

The document is your gateway to finding elements, changing content, and responding to user interactions.

Key Takeaway: window is the browser's global object controlling the environment; document (accessed via window.document) is your entry point to the page's DOM structure.