Skip to main content

Shortest Program Ever

Published 3 min readMarkdown
Shortest Program Ever

The deeper I study JS, the more I fall in love with this programming language. Let me tell you something very interesting, and I bet no other programming language has this.

How to run a JS program even without a single line of code or word.

The shortest possible JavaScript program is simply an empty file or a file containing just a semicolon ;. Though this might seem trivial, there’s a lot happening under the hood.

In this blog, we'll explore the fascinating world of JavaScript, starting from this minimal program and diving into concepts like the Global Execution Context, the window object, the this keyword, and how variables and functions are managed in the Global Memory Space.

The shortest valid JavaScript program could look like this:

;

Or even an empty file! But what does JavaScript do with such a file? Despite the apparent lack of code, the JavaScript engine sets up an environment to run the script. This environment is known as the Global Execution Context.

Behind the Scenes: Global Execution Context and the window Object

When you run a JavaScript program in a browser, the JavaScript engine creates something called the Global Execution Context. This context is the default environment where all JavaScript code is executed.

The Global Execution Context consists of two main components:

  1. Global Object: In a browser, this is the window object.

  2. this Keyword: In the global context, this refers to the Global Object, which is the window object in browsers.

Even if your JavaScript program is empty, the Global Execution Context is still created, and the window object is set up with various methods and properties that you can use.

Here’s an example of how the window object works:

console.log(window); // Outputs the global window object
console.log(this);   // Also outputs the global window object

In the global scope, this refers to window, so console.log(this) and console.log(window) are equivalent.

The this Keyword in the Browser

The this keyword can be tricky, as its value depends on where it is used. In the global context, as we've seen, this refers to the window object.

Here’s an example to illustrate:

console.log(this === window); // true

However, inside functions or objects, this might refer to something else. Let’s explore this with an example:

function showThis() {
    console.log(this);
}

showThis(); // Outputs: window

const obj = {
    name: "JavaScript",
    logThis: function() {
        console.log(this);
    }
};

obj.logThis(); // Outputs: obj

In the global context, this is window, but inside obj.logThis, this refers to obj.

Variables and Functions in the Global Memory Space

When you declare variables and functions globally, they are automatically added to the window object. This is part of the global memory space, where JavaScript keeps track of all global variables and functions.

var globalVar = "I'm global!";
function globalFunc() {
    console.log("I'm a global function!");
}

console.log(window.globalVar); // Outputs: "I'm global!"
window.globalFunc();           // Outputs: "I'm a global function!"

Even though we declared globalVar and globalFunc with var and function, they are accessible through the window object. This behavior is specific to the global scope.

Conclusion

Even with an empty file, when the engine runs the JavaScript file, the Global Execution Context, the window object, the this keyword, and the Global Memory Space are created. They all play crucial roles in how JavaScript executes code in the browser.

No comments yet. Be the first to comment.

More from this blog