Flat Preloader Icon

Window Object

The window object is a fundamental part of JavaScript’s client-side functionality. It represents the browser window or a frame, and it acts as a global object for all JavaScript code running within that window. Here’s some essential information about the window object in JavaScript:

1.Global Scope: In a web browser environment, variables and functions declared globally (outside of any function) become properties and methods of the window object. This means you can access them using dot notation, like
					window.variableName or window.functionName().

2.Browser Information:

The window object provides properties and methods to interact with the browser window and its contents. For example, you can access information about the browser’s dimensions, history, location, and more. Some common properties include window.innerWidth, window.innerHeight, window.location, and window.history.

3.DOM Access:

The window object serves as the global scope for the Document Object Model (DOM). This means that DOM elements, such as document and navigator, are accessible directly through the window object. For example, you can access the document object using window.document or simply document.
4.Timers and Events:

JavaScript’s setTimeout() and setInterval() functions, used for executing code after a specified delay or repeatedly at a set interval, are part of the window object. Additionally, event handling methods like addEventListener() are also part of window.

Here are some important methods of the window object:

open(): Opens a new browser window or a new tab, optionally with specified URL, dimensions, and other properties.

					window.open("https://example.com", "_blank", "width=500,height=500");


close(): Closes the current browser window if it was opened with JavaScript.



alert(): Displays an alert dialog box with a specified message.

					window.alert("This is an alert!");


confirm(): Displays a dialog box with a message and OK and Cancel buttons. Returns true if the user clicks OK, and false if they click Cancel.

					if (window.confirm("Are you sure?")) {
    // User clicked OK
} else {
    // User clicked Cancel


prompt(): Displays a dialog box that prompts the user for input. Returns the text entered by the user, or null if the user cancels the dialog.

					let userInput = window.prompt("Please enter your name:", "John Doe");


setTimeout(): Executes a function or a specified piece of code once after a specified delay (in milliseconds).

					setTimeout(function() {
    console.log("This message will appear after 2 seconds.");
}, 2000);


setInterval(): Executes a function or a specified piece of code repeatedly, with a fixed time delay between each execution (in milliseconds).

					setInterval(function() {
    console.log("This message will appear every 5 seconds.");
}, 5000);


clearTimeout(): Cancels a timeout previously set with setTimeout().

					let timeoutID = setTimeout(function() {
    console.log("This message will not appear.");
}, 2000);


clearInterval(): Cancels a recurring action previously set with setInterval().

					let intervalID = setInterval(function() {
    console.log("This message will not appear repeatedly.");
}, 5000);


These methods provide powerful ways to interact with users, manage time-based events, and control browser windows and tabs dynamically within a JavaScript application.

Share on: