The Window Object is the top-level object in the Browser Object Model (BOM). It represents the browser window or tab that displays a web page.In JavaScript, the window object is created automatically by the browser. All global JavaScript objects, functions, and variables become properties of the window object.
var siteName = "Learn2Kode";
function showName() {
console.log(siteName);
}
console.log(window.siteName); // Learn2Kode
window.showName(); // Calls function
console.log(window.innerWidth);
console.log(window.innerHeight);
console.log(window.location.href);
// Redirect user
window.location.href = "https://learn2kode.in";
window.history.back(); // Go back
window.history.forward(); // Go forward
console.log(screen.width);
console.log(screen.height);
alert("Welcome to Learn2Kode!");
confirm("Are you sure?");
prompt("Enter your name:");
| Method | Purpose |
|---|---|
| alert() | Show message |
| confirm() | OK / Cancel dialog |
| prompt() | Take user input |
window.open("https://learn2kode.in", "_blank");
window.close();
setTimeout(() => {
console.log("Executed after 2 seconds");
}, 2000);
setInterval(() => {
console.log("Repeats every second");
}, 1000);
| Feature | Window Object | Document Object |
|---|---|---|
| Controls | Browser | Web page |
| Scope | Global | Page-specific |
| Example | window.alert() |
document.getElementById() |
Use the Window Object when you need to: