All 28 lessons ▾
Inside JavaScript · Lesson 5 of 28
The Shortest JavaScript Program: window and this
So, what's the shortest JavaScript program you can write? One line? One word? It's even shorter than that! And it secretly creates three things. So let's see what they are.
A new lesson goes up every day at 11 am. Subscribe on YouTube so you don't miss it.
Got a doubt? Ask in the comments under the video.Subscribe
- The shortest JavaScript program is an empty file
- It still gets a global execution context
- The engine creates window, the global object
- At the global level, this === window
- Global var and function become properties of window
- window in browsers, global in Node, globalThis everywhere
Part 1The shortest program
So quick question. What's the shortest JavaScript program in the world? A console log? A single variable? No! So the shortest JavaScript program is an empty file. Nothing in it at all. Zero lines. Nothing.
1// nothing here2// not a single line of codeSo here it is. An empty file. Now you might think, if there's no code, JavaScript does nothing. Wrong! Even for this empty file, the JavaScript engine does a lot of work behind the scenes. Now remember the one rule from episode one? Everything in JavaScript happens inside an execution context. So even with zero lines, the engine still creates the global execution context. Memory, and code. It's just that there's nothing to run.
So remember this. Even an empty file gets a global execution context. And it doesn't come alone. Along with it, the engine creates two more things for you, for free. So let's find out what they are.
Part 2Gift number one: window
So the first gift is a very special object. Let's see it.
Now, along with the global execution context, the engine creates a global object. In the browser, this object is called window. And it's huge! It's full of ready-made functions and variables. setTimeout, alert, local storage, the document, and many more. Now tell me, where do you think setTimeout comes from? You never wrote it. So it lives inside window, and you can use it from anywhere in your program. So the engine creates the global context, and window comes with it.
Now try this yourself. Open an empty page in your browser, open the developer tools, go to the console, and type window. Press enter. You'll see a giant object, with hundreds of things inside it. And your page has no code at all! So the engine made all of that, before your first line could even run. Scroll through it, and you'll spot alert and setTimeout sitting right there. That's the global object.
Part 3Gift number two: this
Now, the second gift. And this one has a funny name. It's called this.
So along with window, the engine also creates the this keyword. And at the global level, this points to the window object. So if you type this equals equals equals window in the console, you get true. They're the same object! Now here's a small secret. It's not only the global context. Every execution context, even a function's, gets its own this. We'll have a full episode on this later in the course, because inside functions it changes a lot. But at the top level of your file, this is simply window.
1console.log(this === window);2console.log(window === globalThis);true true
Don't just trust me. Let's prove it with code. So line one checks if this is exactly the same as window. And the console says true. Now line two checks window against something called globalThis, which we'll meet in a minute. True again! Two lines, and you've proved it yourself. Isn't that nice?
So let me summarize. For every JavaScript program, even an empty file, the engine creates three things. The global execution context. The global object, window. And this, which points to window. Three things. Very important, so remember them.
Part 4Your variables live on window
Now here's the most interesting part. What happens to the variables you create at the top level of your file? Let's see.
1var course = "Inside JavaScript";2function hello() {}3 4console.log(course);5console.log(window.course);6console.log(this.course);Inside JavaScript Inside JavaScript Inside JavaScript
So look at this program. A var called course, and a function hello, both in the global space. Now phase one stores them in memory, as always. Line one, and course becomes Inside JavaScript. Line four logs course, and it prints Inside JavaScript. No surprise. Now line five logs window.course. And it prints the same thing! Line six logs this.course, and once again, Inside JavaScript. So what's going on? When you create a var or a function in the global space, it gets attached to the window object. So course is also window.course, and since this is window, it's also this.course.
So remember this. Global space means any code that is not inside a function. And a var or a function you create in the global space gets attached to window. Your course variable is literally sitting inside window. And when you write just course, without window dot, JavaScript assumes you mean the global one.
1function lunch() {2 var food = "dosa";3}4lunch();5 6console.log(window.food);7console.log(food);undefined ReferenceError: food is not defined
But now, what about a variable inside a function? Look. lunch has its own var food. Line four calls lunch, and food lives in lunch's own memory, until lunch returns. Then line six logs window.food. And it prints undefined. Why? Because food was never in the global space. It belonged to lunch's own box, not to window. Now line seven logs just food, with nothing in front of it. So JavaScript looks for food in the global space, can't find it anywhere, and throws an error. food is not defined! See the difference? Asking window for a property it doesn't have quietly gives undefined. But plain food crashes with not defined. Remember this, because the next episode is all about it. Only global variables go on window.
1var name = 10;2 3console.log(typeof name);string
Now here's a crazy one, and it shows why global variables can be dangerous. Line one, var name equals ten. A number. Now line three asks for the type of name. And the console says string! Not number. Why? Because window already has a special property called name, and it always stores a string. So our global var name became window.name, and our ten was turned into the string ten. You created a variable, and it clashed with something inside window you didn't even know existed.
So here's the real lesson. Every global var lands on window, where it can clash with the browser's own things, or with another script on the same page. That's why good developers keep global variables to a minimum, and put their variables inside functions. So, fewer globals, fewer surprises. Very important for real projects.
Part 5Not just browsers
Now, one more important thing. JavaScript doesn't only run in browsers. It runs on servers too.
So wherever JavaScript runs, there's a JavaScript engine running it. V8 in Chrome, SpiderMonkey in Firefox, JavaScriptCore in Safari. And every one of these engines has the same duty. It must create a global object. But now, that object's name can be different. In the browser, it's window. In Node.js, on a server, it's called global. And because that got confusing, modern JavaScript added one name that works everywhere. globalThis. So in a browser, globalThis is window. In Node, globalThis is global. So if you want code that runs anywhere, use globalThis. That's a small bonus tip for you.
And now a quick peek ahead. If you create a variable with let or const at the top level, it does not go on window. Only var and function do. Why? Well, that's a story for episode eight, where we meet let, const, and something called the temporal dead zone. Sounds scary. But it's not.
1var level = 5;2function play() {3 var lives = 3;4}5play();6 7console.log(window.level);8console.log(window.lives);What do the last two lines print?
Show the answer
Let's see. level is a global var, so it lives on window. So line seven prints five. And lives? It was created inside play, in play's own memory. It never touched window. So line eight prints undefined. Did you get both right? Great job!
Did you know setTimeout was living inside window all this time?
Answer in the comments under the video.
The video on YouTube, and the lesson as a swipe-through on Instagram.