Shortest JS Program

Neha Prabhakar
3 min readApr 14, 2024

--

Hey There! The title of the topic is weird right? What do I mean by shorted JS program? Of course if someone prints Hello world on the console, it will be a 1 line shortest code! What if I tell you that you’re wrong on this one :( Let me tell you why :P

Photo by Kelly Sikkema on Unsplash

Below is the screenshot of the shortest JS program. Make sure to copy the code exactly as it is else it won’t execute.

No the picture is correctly uploaded, it’s just that the shortest program in JS is an empty .js file. When this file is executed a Global Execution Context (GEC) is created to understand the GEC concept in more detail please go through my blog: https://medium.com/@nehaprabhakar008/basics-of-javascript-257898cc6c80. Along with the GEC a window is created.

Now the question is what is a Window?

Photo by Nicolas Solerieu on Unsplash

Yes the above picture is also a window but the window we are talking about in JS is actually a global object which is created along with Global Execution Context. It has different programs which are globally accessible in the program. These methods when required can be used by us.

Now there is another thing which is created along with GEC and window i.e., this keyword which is pointing to the window.

At this point of time i.e., the base level:
this === window (=== means equivalent)

Let’s take and example to understand the above terms more clearly :

Output for the above will be :
10
10
10
error

As shown in the above diagram when GEC is created a window is created in it and it contains the value of a now if you print that value of a as simple a or window.a or this.a they all mean the same and will print 10. Now coming to x as it was defined in local memory and it got deleted from the call stack it isn’t accessible in the global memory now.

Hope this was helpful and easy to understand. If you found this blog helpful please go through my profile and have a look at some other interesting content. See you in next one! :) Until then..

Photo by Brigitte Tohm on Unsplash

--

--