In a browser, the window object is the global object. It represents the browser window or tab and provides access to important browser features such as size, dialogs, timers, location, and the loaded document.
Any global variable, function, or object you create in a script becomes a property of window. Most of the time you can omit the explicit window. prefix because it is implied in the global scope.
window is the root object for client-side JavaScript in browsers.window.this in the global scope also refers to window.window.innerWidth – Width of the viewport (content area) in pixels.window.innerHeight – Height of the viewport in pixels.window.document – The DOM of the current page.window.location – Information about (and control over) the current URL.window.screen – Information about the user’s screen/device.window.navigator – Information about the browser and operating system.alert(message) – Shows an alert dialog.confirm(message) – OK/Cancel dialog; returns true or false.prompt(message, default) – Dialog that lets the user type a value.open(url, name, specs) – Opens a new window or tab.close() – Closes the current window (if opened by script).setTimeout() – Runs code once after a delay.setInterval() – Runs code repeatedly at fixed intervals.window are effectively the same.window.yourFunction.window object.You can access window members either implicitly (just by using their names) or explicitly with the window. prefix:
<script>
var x = 5;
// Implicit access
console.log(x);
// Explicit access through window
console.log(window.x); // Output: 5
</script>
The same applies to global functions – they are also stored as properties of window.
<script>
function greet() {
console.log("Hello");
}
greet(); // Direct call
window.greet(); // Equivalent call
console.log(window.greet); // Logs the function definition
</script>
<script>
console.log(window.innerWidth); // e.g., 1280
console.log(window.document.title); // Page title
console.log(window.location.href); // Current page URL
</script>
<script>
let username = prompt("What is your name?");
if (confirm("Do you want to proceed?")) {
alert("Welcome, " + username);
} else {
alert("Action cancelled.");
}
</script>
<script>
let newWin = window.open(
"https://example.com",
"_blank",
"width=400,height=300"
);
newWin.focus(); // Bring new window to front
newWin.close(); // Close it (allowed only if opened by script)
</script>
<script>
console.log("Viewport width: " + window.innerWidth);
console.log("Viewport height: " + window.innerHeight);
</script>
In the dialog example:
prompt() shows a dialog asking for the user’s name and returns the typed value.confirm() then asks whether to proceed. It returns true if the user clicks OK, or false if they click Cancel.confirm() returns true, alert() displays a welcome message using the entered name; otherwise it shows a cancellation message.In the viewport example, the values printed in the console will change if you resize the browser window and run the code again (or listen to the resize event).
window.location.setTimeout / setInterval.window.document.Click the button below to read the current viewport size using window.innerWidth and window.innerHeight.
Click the button to see the current width and height of the window.
| Property / Method | Description |
|---|---|
window.innerWidth |
Width of the viewport (content area). |
window.innerHeight |
Height of the viewport. |
window.alert() |
Shows an alert dialog. |
window.open() |
Opens a new window or tab. |
setTimeout() |
Runs code once after a specified delay. |
setInterval() |
Runs code repeatedly at a fixed interval. |
window.location |
Provides URL information and navigation methods. |
window.document |
Gives access to the page DOM. |
window. when calling most global functions like alert() and setTimeout().window; avoid polluting the global scope.window.open() sparingly – many browsers block unwanted pop-ups.window.onresize or addEventListener("resize", ...) to react to window size changes.window.close() on windows/tabs that were opened by your script.setInterval() or long-lived setTimeout() using clearInterval() / clearTimeout().window in environments like Node.js where it does not exist.innerWidth and innerHeight for viewport size instead of window.screen.prompt() to ask the user’s age and display it in an alert().setInterval() that starts from 10 and stops at 0 using clearInterval().window.open() and automatically close it after 5 seconds using setTimeout().window object using console.log(window) and explore what you see.