Window Object in JavaScript

Javascript 7 min min read Updated: Mar 09, 2026 Intermediate
Window Object in JavaScript
Intermediate Topic 13 of 15

In JavaScript, the window object is the global object that represents the browser window. It is the top-level object that contains all the built-in properties, methods, and variables available in a web browser environment.

Whenever JavaScript runs in a browser, it automatically creates the window object. All global variables and functions defined in a script become part of this object.

What is the Window Object?

The window object represents the browser window and provides access to important browser features such as alerts, timers, location information, and navigation.

Key Point: The window object is automatically created by the browser and acts as the global object in JavaScript.

Accessing the Window Object

JavaScript automatically provides access to the window object, so developers usually do not need to write the word window. However, it can still be used explicitly.

javascript window.alert("Welcome to JavaScript Tutorial");
Output

A browser alert box appears displaying the message.

The same code can also be written without using the window keyword.

javascript alert("Welcome to JavaScript Tutorial");
Output

The browser displays the alert message.

Common Window Object Methods

The window object provides many built-in methods used in web development.

  • alert() – displays a message box
  • prompt() – asks the user for input
  • confirm() – displays a confirmation dialog
  • setTimeout() – executes code after a delay
  • setInterval() – executes code repeatedly at intervals

Example of prompt()

javascript let name = prompt("Enter your name"); console.log(name);
Output

The user enters a value in the prompt dialog and it is displayed in the console.

Window Object Properties

The window object also contains properties that provide information about the browser window.

  • window.innerWidth – returns the width of the browser window
  • window.innerHeight – returns the height of the browser window
  • window.location – provides information about the current URL
  • window.document – represents the webpage loaded in the browser
javascript console.log(window.innerWidth);
Output

The width of the browser window is displayed in pixels.

Key Point: The document object is part of the window object and represents the webpage.

Why the Window Object is Important

The window object provides access to many browser features that allow JavaScript to interact with the browser environment. It acts as the global container for all JavaScript functions and variables running in the browser.

Most browser-related operations such as navigation, dialogs, timers, and screen information are handled through the window object.

Conclusion

The window object is the global object in browser-based JavaScript. It provides access to many built-in browser features and contains methods such as alert(), prompt(), and confirm().

Understanding the window object helps developers control browser behavior and interact with the user more effectively.

In the next tutorial, you will learn about Navigator Object in JavaScript, which provides information about the user's browser and device.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators