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.
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.
A browser alert box appears displaying the message.
The same code can also be written without using the window keyword.
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()
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
The width of the browser window is displayed in pixels.
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.

