Shortest JS Program, Window & this keyword 🔥

ramya_srim

Ramya Sri M

Posted on November 27, 2024

Shortest JS Program, Window & this keyword 🔥

Shortest JS Program 🤔

The shortest program in JavaScript is an empty file. Although the file is empty and there is nothing to execute, the JavaScript engine still performs several tasks behind the scenes. If we run the shortest JavaScript program (i.e., an empty file), the engine creates a global execution context and allocates memory space. Even with nothing to set up, it is still doing its job.

window & this keyword

  • The JavaScript engine creates something known as the window object. The window object is the global object that is created alongside the global execution context. It contains functions and variables, which are attached to the global object (i.e., window). All JavaScript engines are responsible for creating the global object. In the case of browsers, this global object is the window.

  • Along with the execution context, the JavaScript engine also creates the 'this' keyword. At the global level, this points to the window object in the case of a browser.

this === window

var a = 11;
console.log(window.a); //11
console.log(a);        //11 (If we don't specify window (window.a), it automatically assumes that we are referring to the global space.)
console.log(this.a);   //11
Enter fullscreen mode Exit fullscreen mode

These variables and functions are attached to the global object (i.e., window). If we create a variable in the global scope, it gets attached to the global object.

Credits to Akshay Saini. [(https://youtu.be/QCRpVw2KXf8?si=5YAjXNQ0wxvCo96Z)]

💖 💪 🙅 🚩
ramya_srim
Ramya Sri M

Posted on November 27, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related