Shortest JavaScript Program 😍
The shortest JS Program is an empty program. When we run an empty JavaScript code, a global execution context is created. The JS Engine sets up the global execution context and global memory space even though there is no code. 🤔
In addition, the JS engine creates a global object called "Window" in the browser environment, which contains global variables and functions. 🌐
The global object can be accessed using the Window keyword or the this keyword at the global level (or at the global level, this === window). 💻
console.log(this === window); // true
What is the window Object?
The window object is a global object that represents the browser window. It is the top-level object in the browser's JavaScript environment, and it is the global object for JavaScript code running in the browser.
What Does the window Object Contain?
The window object contains various functions, variables, properties, and methods. These are created in the global memory space, meaning you can access these variables and functions from anywhere in the JavaScript program.
Key Features of the window Object:
1. Global Variables & Functions:
Any variable declared in the global scope becomes a property of the window object. Similarly, functions declared globally become methods of the window object.
Example:
var a = 10;
function greet() {
console.log("Hello from the global function!");
}
// Accessing the variable and function through window object
window.a; // 10
window.greet(); // "Hello from the global function!"
2. Built-in Global Functions:
The window object includes several useful global functions that are available in any JavaScript program running in a browser:
alert(): Displays an alert box to the user.
console.log(): Logs output to the console.
setTimeout(): Executes a function after a specified delay.
setInterval(): Repeatedly executes a function at regular intervals.
Example:
window.alert("Hello, World!");
window.console.log("This is a log message");
3. Interacting with the Browser Environment
The window object provides access to the browser environment, allowing you to interact with the browser and its features. For example, you can access the browser's location, history, and document object through the window object.
Recommended by LinkedIn
Manipulating the DOM (Document Object Model) via window.document
Managing the browser’s history through window.history
Accessing the current URL with window.location
Displaying alerts, prompts, and confirmations with window.alert, window.prompt, and window.confirm
Opening new browser windows or tabs using window.open
Storing data using window.localStorage and window.sessionStorage
window.document.title = "New Title"; // Change the document title
window.location.href = "https://www.example.com"; // Redirect to a new URL
window.alert("Hello, World!"); // Display an alert message
window.open("https://www.example.com"); // Open a new browser window
4. Handling Events:
You can use the window object to add event listeners that respond to browser events like resizing the window, scrolling, or clicking:
window.addEventListener("resize", function() {
console.log("Window was resized!");
});