Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Window and this keyword ?
Utkarsh Yadav
Utkarsh Yadav

Posted on • Edited on

     

Window and this keyword ?

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 underwindow keyword object orGlobal State Object.

Let's see an example to understand it better.

vara=10;// Global spacefunctionb(){// Global spacevarx=10;// Not in Global space}console.log(window.a);// Global spaceconsole.log(a);// Global spaceconsole.log(x);// Global space
Enter fullscreen modeExit fullscreen mode

Points to be noted:

  • Everything that is inside Global Space are accessed insidewindow object.
  • Everything that is outside Global Space are not accessed insidewindow 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!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Ask me about Typescript, JavaScript, React, Next
  • Location
    Germany
  • Education
    Technische Universität Chemnitz
  • Pronouns
    he/him
  • Work
    Ex- Cognizant
  • Joined

More fromUtkarsh Yadav

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp