The window Object and this Keyword
The window object is the global container in browsers. var attaches to it, let and const do not. The this keyword points to different things depending on context.
var variables become properties of window. let and const live in a separate memory space. At global level, this === window. In strict mode functions, this is undefined.
EXERCISE
1The window Object: JavaScript Globals
Every browser gives JavaScript a massive object called window. It holds web APIs, global variables (declared with var), and serves as the global context.
Every environment provides one object that holds the built-in available everywhere. In a browser that object is called window. It is not part of the JavaScript language; it is what the browser hands the engine when a page loads.
Most of what you call daily lives on it, which is why you can use those names without ever mentioning window:
alert("Hello"); // window.alert
console.log("Hi"); // window.console.log
setTimeout(handler, 1000); // window.setTimeout
Any name not found in your own scopes is looked up on the global object as a last step, which is what makes the short forms work.
Only var adds a property to that object:
var brand = "DevLoom";
let version = 2;
const year = 2025;
console.log(window.brand); // "DevLoom"
console.log(window.version); // undefined
console.log(window.year); // undefined
A top-level var creates a property on window. A top-level let or const creates a binding in a separate scope that window cannot see. This is one of the concrete reasons the newer keywords were added: a var at the top of any script shares one namespace with every other script on the page.
The failure mode is worth seeing, because it is silent:
var config = { mode: "dark" };
// somewhere else entirely, in another file on the same page
var config = { retries: 3 };
console.log(config); // { retries: 3 }
Both files declared config at the top level, so both wrote to the same property on window. The second one replaced the first, no warning was issued, and the first file's feature now reads a configuration object that does not have the key it expects. With let, the second declaration would be a SyntaxError and you would find out immediately.
Pro Tip: In Node.js the global object is
globalThis, and there is nowindowordocumentbecause there is no browser.globalThisworks in both, which is why you will see it in code meant to run in either place.
EXERCISE
2The this Keyword at Global Level
At the global level (outside any function), this points to the window object. They are the same thing.
this is a name whose value is decided when a function is called, not when it is written. That is the opposite of every other name in JavaScript, and it is the entire reason this has a reputation. At the top level of a script, though, it is completely predictable.
At the top level, this is the global object:
console.log(this === window); // true
They are the same object. Anything you can reach through one, you can reach through the other.
Inside an ordinary function call, the answer depends on strict mode:
function showThis() {
console.log(this === window); // true
}
showThis();
Without strict mode, calling a plain function sets this to the global object. There is no good reason for this behaviour; it exists because the language could not change it without breaking older pages.
"use strict";
function showThis() {
console.log(this); // undefined
}
showThis();
With strict mode, this is undefined instead. That looks less helpful and is considerably safer. Under the old rule, a method that lost its receiver would quietly start reading and writing properties on the global object instead of failing:
"use strict";
const counter = {
count: 0,
increment() {
this.count++;
},
};
const fn = counter.increment;
fn(); // TypeError: Cannot read properties of undefined
Pulling increment out of the object detaches it from counter. In strict mode the mistake throws at the line that caused it. Without strict mode it would have created a global count of NaN and carried on, and you would have found out much later that counter.count never moved.
Modules and class bodies are strict automatically, so most modern code already has the safer behaviour whether or not anyone wrote the directive.
Arrow functions are the exception to all of this, and the exception is the useful part:
"use strict";
const timer = {
label: "upload",
start() {
setTimeout(() => {
console.log(this.label); // "upload"
}, 100);
},
};
timer.start();
An arrow function has no this of its own. It uses whatever this was in the scope where it was written, which here is the one belonging to start. Written as a regular function, the callback would have been called by the timer with no receiver, and this.label would have thrown.
EXERCISE
3Web APIs: The Superpowers That Live on window
console.log, setTimeout, fetch, document, localStorage... these are NOT part of JavaScript. They are browser features that JavaScript accesses through the window object.
console.log is not part of JavaScript. Neither is setTimeout, fetch, or anything beginning with document. They are provided by the environment the engine is running in, and the distinction matters more than it sounds like it should.
What belongs to the language, and what is handed to it:
const x = 10;
function add(a, b) {
return a + b;
}
if (x > 5) {
add(1, 2);
}
Declarations, functions, conditionals, loops, operators, objects, arrays: all defined by the ECMAScript specification and identical in every environment.
console.log("Hi");
setTimeout(handler, 1000);
document.getElementById("app");
fetch("https://example.com");
localStorage.setItem("key", "value");
None of these appear in that specification. They are browser features, exposed on window, and JavaScript reaches them the same way it reaches anything else on the global object.
This is not trivia, because it explains something that otherwise looks impossible. JavaScript runs on a single thread and does one thing at a time, yet a timer appears to wait in the background:
console.log("first");
setTimeout(function () {
console.log("third");
}, 2000);
console.log("second");
The output is first, second, third. The engine did not wait two seconds, and it did not run anything in parallel. It handed the timer to the browser, which counted independently, and carried straight on to the next line. When the two seconds elapsed, the browser gave the function back to be run.
The asynchronous work happened outside JavaScript entirely. That is the mechanism behind every non-blocking operation you will write, and Unit 6 takes apart exactly how the callback finds its way back.
Change the environment and the available features change with it:
// Node.js
const fs = require("fs");
const http = require("http");
// no window, no document, no localStorage
provides a file system, servers and processes, and provides no DOM, because there is no page. The language is the same in both; the surroundings are not.
Pro Tip: When something works in the browser and fails in Node, or the reverse, check whether the thing you are calling is JavaScript or an environment feature. The error will usually say
X is not defined, which is the global object telling you it has never heard of it.
