Window Object
The Window object represents the browser window in which a web page is displayed.
It is the top-level (global) object in the Browser Object Model (BOM).
All global JavaScript objects, functions, and variables become members of the window object.
Global Scope and Window Object
Any global variable or function becomes a property of window.
Global variables belong to window
Returns true or false
Returns user input or null
Console Output Using Window
Gets viewport size
location (Access URL)
Redirect page
Accessing Other Browser Objects via Window
Window is the parent of all BOM objects
Window Object Hierarchy
Difference Between Window Object and Document Object
The window object controls the browser window.
Why the Window Object is Important
The Window object allows JavaScript to:
- Display alert boxes
- Access browser information
- Control navigation
- Manage timers
- Access other browser objects (history, location, navigator)
Global Scope and Window Object
Any global variable or function becomes a property of window.
var name = "JavaScript";console.log(window.name);Global variables belong to window
Common Window Object Methods
1. window.alert()
Displays an alert popup.
window.alert("Welcome to JavaScript");- Used for notifications
- Stops execution until user responds
2. window.confirm()
Displays a confirmation dialog.
let result = window.confirm("Are you sure?");console.log(result);Returns true or false
3. window.prompt()
Displays an input dialog.
let userName = window.prompt("Enter your name");console.log(userName);Returns user input or null
Console Output Using Window
window.console.log("Hello Console");- Used for debugging
- window keyword is optional
Window Timers
4. setTimeout()
Executes code after a delay.
setTimeout(() => { console.log("Executed after 2 seconds");}, 2000);5. setInterval()
Executes code repeatedly.
setInterval(() => { console.log("Runs every second");}, 1000);Window Properties
innerWidth and innerHeight
console.log(window.innerWidth);console.log(window.innerHeight);Gets viewport size
location (Access URL)
console.log(window.location.href);Redirect page
window.location.href = "https://example.com";Accessing Other Browser Objects via Window
window.history.back();window.navigator.userAgent;window.screen.width;Window is the parent of all BOM objects
Window Object Hierarchy
window ├── document ├── history ├── location ├── navigator └── screenDifference Between Window Object and Document Object
| Feature | Window Object | Document Object |
|---|---|---|
| Represents | Browser window | HTML document |
| Part of | BOM | DOM |
| Controls | Browser behavior | Page content |
Common Use Cases
- Show alerts and popups
- Handle browser navigation
- Set timers
- Access screen and URL info
- Debug applications
Two Minute Drill
- Window represents the browser window
- It is the top level BOM obect
- alert, confirm, prompt are window methods
- Timers are managed by window
- Other browser objects come under window
- Global scope belongs to window
Need more clarification?
Drop us an email at career@quipoinfotech.com
