Shortest Javascript Program , window and this

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 to that JS e…


This content originally appeared on DEV Community and was authored by Magnus

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 to that JS engine do something interesting, It also create window object.
Image description

How do this window object came out?

This window object is created by JS Engine which has many functions and variables. These are created in global memory space. So, we can access these variables and functions anywhere in Javascript program.

Image description

In addition to that JS Engine will also create this variable. At the global level this points to window object .

Image description

So,what is window?

Window is a global object which is created along with the global execution context.

Whenever any JS Program is run, a global execution context is created ,window object and along with the global execution context a this variable is created.

Let us now know more about global object created.

So, the global object in case of browser is called as window. Javascript not only runs on browsers. It runs on servers and a lot of other devices and places. Wherever Javascript is running, there must be a javascript engine there. Just like in chrome it is v8, Mozilla has it own. Likewise Safari, Edge has it own. So all these JS engine has the responsibility to create a global object. In case of browser it is called as window. In case of node it is called something else. Whereever you runs a Javascript, the names are different but always there is common features i.e a global object is created. Even though our file is empty JS will create a global object.

At the global level this===window is true.

Image description

As we all know, when global execution context is created, along with it global memory space is created. We can view it through developer tools in scope tab.

So, What is global memory space?

Any code which is not inside a function is in global memory space. In a simple way we can say that anything which is not inside a function is in global memory space.

function b()
{
    var c=20; //Not in global memory space
}

If we debug the above code. We will see the variable and functions which are in global memory space are in windows object and not c=20 which is not in global space. So, whenever we create variable and functions in global space, these gets attached in window object.

Image description

var a=10;
function b()
{
    var x=10;
}
console.log(window.a);
console.log(a);
console.log(x);
console.log(this.a);
/*
Output:
10
10
Not defined error as it is not in global
10
*/

Whenever we try to access any functions and variables in our program and if we do not put anything infront of it ,it assumes that it is in the global space.

So, we can come to a conclusion that window.a ,a and this.a all points to the window object I.e all are referring to the same place in the memory space.

Reference:@akshaymarch7


This content originally appeared on DEV Community and was authored by Magnus


Print Share Comment Cite Upload Translate Updates
APA

Magnus | Sciencx (2021-12-01T11:56:20+00:00) Shortest Javascript Program , window and this. Retrieved from https://www.scien.cx/2021/12/01/shortest-javascript-program-window-and-this/

MLA
" » Shortest Javascript Program , window and this." Magnus | Sciencx - Wednesday December 1, 2021, https://www.scien.cx/2021/12/01/shortest-javascript-program-window-and-this/
HARVARD
Magnus | Sciencx Wednesday December 1, 2021 » Shortest Javascript Program , window and this., viewed ,<https://www.scien.cx/2021/12/01/shortest-javascript-program-window-and-this/>
VANCOUVER
Magnus | Sciencx - » Shortest Javascript Program , window and this. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/01/shortest-javascript-program-window-and-this/
CHICAGO
" » Shortest Javascript Program , window and this." Magnus | Sciencx - Accessed . https://www.scien.cx/2021/12/01/shortest-javascript-program-window-and-this/
IEEE
" » Shortest Javascript Program , window and this." Magnus | Sciencx [Online]. Available: https://www.scien.cx/2021/12/01/shortest-javascript-program-window-and-this/. [Accessed: ]
rf:citation
» Shortest Javascript Program , window and this | Magnus | Sciencx | https://www.scien.cx/2021/12/01/shortest-javascript-program-window-and-this/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.