Window and this keyword, what the heck it is?

Table of content

Shortest program in JavaScript
window keyword
This keyword
Working behind the scenes

What is the shortest program in JavaScript ?

Running an empty file in JavaScript is the shortest program in JavaScript.

Create a Java…


This content originally appeared on DEV Community and was authored by Utkarsh Yadav

Table of content

  • Shortest program in JavaScript
  • window keyword
  • This keyword
  • Working behind the scenes

What is the shortest program in JavaScript ?

Running an empty file in JavaScript is the shortest program in JavaScript.

Create a JavaScript file with .js extension and compile the file using Dev Tools in the browser and the magic you would see that even though you have not written a piece of code but your JavaScript engine in the browser will create a complete new Global Execution context with all the methods and API available to you by your browser.

Isn't it interesting?.

Window Keyword

This is a functionality provided by JavaScript engine. basically window is the class in which various functions and methods are encapsulated.

These functions and methods can be used anywhere inside our JavaScript program.

This Keyword

This is an another functionality provided by JavaScript engine. At the Global level this points to window keyword and their functionality.

This is how JavaScript runs --> A global Context is created --> window object is created on initialised by the browser --> the this variable points to the window is created and the complete shortest program of JavaScript runs.

Working behind the scenes.

Steps are:

  • Global Space creation: Anything not inside and block scope or a function scope is said to be in or bounded with Global Space.

Window keyword is Global Space.

So, everything outside the function will be under window keyword object or Global State Object.

Let's see an example to understand it better.

var a = 10;  // Global space
function b() {  // Global space
  var x = 10;  // Not in Global space
}
console.log(window.a);  // Global space
console.log(a); // Global space
console.log(x); // Global space

Points to be noted:

  • Everything that is inside Global Space are accessed inside window object.
  • Everything that is outside Global Space are not accessed inside window object.

Conclusion: This vs window points to the same global space.

This concludes the blog if you like the content please support me:

Read more blogs: utkarshwhocodesblogs

Happy Coding!


This content originally appeared on DEV Community and was authored by Utkarsh Yadav


Print Share Comment Cite Upload Translate Updates
APA

Utkarsh Yadav | Sciencx (2021-06-17T15:04:42+00:00) Window and this keyword, what the heck it is?. Retrieved from https://www.scien.cx/2021/06/17/window-and-this-keyword-what-the-heck-it-is/

MLA
" » Window and this keyword, what the heck it is?." Utkarsh Yadav | Sciencx - Thursday June 17, 2021, https://www.scien.cx/2021/06/17/window-and-this-keyword-what-the-heck-it-is/
HARVARD
Utkarsh Yadav | Sciencx Thursday June 17, 2021 » Window and this keyword, what the heck it is?., viewed ,<https://www.scien.cx/2021/06/17/window-and-this-keyword-what-the-heck-it-is/>
VANCOUVER
Utkarsh Yadav | Sciencx - » Window and this keyword, what the heck it is?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/17/window-and-this-keyword-what-the-heck-it-is/
CHICAGO
" » Window and this keyword, what the heck it is?." Utkarsh Yadav | Sciencx - Accessed . https://www.scien.cx/2021/06/17/window-and-this-keyword-what-the-heck-it-is/
IEEE
" » Window and this keyword, what the heck it is?." Utkarsh Yadav | Sciencx [Online]. Available: https://www.scien.cx/2021/06/17/window-and-this-keyword-what-the-heck-it-is/. [Accessed: ]
rf:citation
» Window and this keyword, what the heck it is? | Utkarsh Yadav | Sciencx | https://www.scien.cx/2021/06/17/window-and-this-keyword-what-the-heck-it-is/ |

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.