Lesson 5 · 9 min read

Window and Global This

Even a completely empty JS file isn't really empty — the engine still builds a global object and points a keyword called this straight at it. Here's exactly what gets built, what it's called in different places, and the one rule that quietly changes depending on strict mode.

On this page

  1. Concept
  2. Why this matters
  3. The hotel before any guest arrives — an analogy
  4. The shortest JS program
  5. One object, many names
  6. this === window at the top level
  7. The shared register — multiple script tags
  8. Strict mode changes the rule — but only sometimes
  9. See it live — console & DevTools
  10. Common misconceptions
  11. Interview questions

Concept

Before you write a single line of code, JavaScript has already done real work: it created a Global Execution Context (Lesson 1), and inside it, two things you didn't ask for — a global object, and a keyword called this that points straight at it. This lesson is entirely about those two things: what they're called, how they relate, and the one rule that changes in strict mode.

Key terms, explained simply

  • Global object is the one object the engine creates for the whole program. Every global var and every top-level function becomes a property of it, and built-in tools such as console and setTimeout are found on it. An object is a collection of named values, and each name and value pair is a property.
  • this is a keyword (a reserved word with a special meaning) that gives your code access to an object. Which object depends on how and where the code runs. At the top level it is the global object.
  • Strict mode is an optional setting that makes JavaScript stricter. You turn it on by writing 'use strict'; at the top of a script or function. It turns some silent mistakes into errors and changes a few rules, such as the value of this in a plain function call.
  • Environment (or runtime) is the place where JavaScript runs and the tools that place gives it. The main ones are a browser, Node.js (a program that runs JavaScript outside a browser, for example on a server) and a Web Worker (a script that runs in the background of a web page, on its own thread, separate from the page).
  • Plain function call means calling a function by its name alone, like greet(), not as a method (obj.greet()) and not with call, apply or bind. Lesson 5 only needs this case.

Why This Matters

  • It explains why var at the top level and a property on window are, underneath, the exact same thing — not a coincidence, a direct consequence of Lesson 3's Object Environment Record.
  • It explains a very real source of production bugs: two separate <script> tags on the same page silently sharing — and overwriting — the same global variables.
  • It's the correct starting anchor for the full this rule set (method calls, arrow functions, call/apply/bind) that gets its own dedicated lesson later — you need the global case solid before the rest makes sense.

The Hotel Before Any Guest Arrives — An Analogy

The building exists before anyone checks in

Picture a brand-new hotel on its first day, before any guest has arrived. The building already exists — a reception counter, an address, a lobby. Nobody built that because of a guest request. It is just there, the moment the hotel opens.

Now imagine someone walks into the empty lobby and asks “who's in charge here?” No specific guest is being addressed, so the answer is the person at the front desk. That default answer is what this is at the global level: when nothing else is specified, it points to the building's own front desk, the global object.

The Shortest JS Program

Take a completely empty .js file. Zero lines of code. JS still does real work behind the scenes: it builds a Global Execution Context, reserves its Variable Environment, creates a global object, and sets this to point at it.

empty.js
// this file is completely empty
// and yet: a Global Execution Context, a global object, and `this` already exist
step 1 / 6
Program starts
The file is loaded — zero lines of code

Nothing to parse, nothing to hoist. And yet the engine does not stop here.

empty.js
// this file is completely empty
// and yet: a Global Execution Context,
// a global object, and `this` already exist
Call stack
[ ] empty
Step through what the engine builds for an empty file. Every piece appears before any code could have asked for it. · interactive

None of this depends on you writing code. The engine always sets it up when a program starts. So the global object and this are not things your code creates. They are given to you.

One Object, Many Names

The global object is the same idea everywhere, but different JavaScript environments give it different names:

EnvironmentName
Browserwindow
Node.jsglobal
Web Worker (and service worker)self
Universal (any environment)globalThis

Because the names differed, JavaScript added globalThis in 2020 (ES2020). It is one name that works in any environment, whether browser, Node or other.

window
browser
global
Node.js
↘↙
the global object
one per program
↗↖
self
Web Worker
globalThis
everywhere
Same object underneath, different local names — except globalThis, which always works. · one object, many names

this === window At The Top Level

routes.js
var x = 10;

console.log(x);        // 10
console.log(this.x);   // 10
console.log(window.x); // 10

All three print the same value. A var at the global level becomes a property x of window (Lesson 3's Object Environment Record), and this at the global level points to that same window object. These are three routes to the same box. This is true in a browser script. See the note below for Node.js and modules.

step 1 / 6
Memory creation phase
Memory phase — x goes onto window

A top-level var lands in the Object Environment Record, which is tied to the global object. So x is now a real property: window.x exists, holding undefined.

routes.js
1var x = 10;
2
3console.log(x); // 10
4console.log(this.x); // 10
5console.log(window.x); // 10
Console
(nothing yet)
Global EC
(no lookup in progress)
ThisBinding
this → window
↓
Object Environment Record · window
  • xundefined
  • console, Math, …built-ins
Step through the three reads. Each one lights up a different path, and every path ends at the same slot on window. · interactive
Not every top level works this way

In a browser <script type="module"> (an ES module), the top-level this is undefined, and a top-level var does not become a property of window. In a Node.js file, a top-level var stays private to that file, and the top-level this is module.exports (an empty object at first), not global. This lesson's examples are for a classic browser script.

The Shared Register — Multiple Script Tags

This part is easy to miss. A <script> tag is the HTML tag that loads JavaScript into a page. If a page loads two separate <script> tags, they don't get their own private globals. They share the same window.

index.html
<script>
  var hotelName = "Grand Palace";
</script>

<script>
  console.log(hotelName); // "Grand Palace" — fully visible here too
  hotelName = "Overwritten!";
</script>
step 1 / 5
Script tag 1
First <script> runs — var hotelName

There is one Global EC and one window for the whole page. The var lands on window, like any top-level var.

index.html · tag 1
<script>
var hotelName = "Grand Palace";
</script>
window · the page's one global object
hotelName"Grand Palace"
console
(nothing yet)
index.html · tag 2
<script>
console.log(hotelName);
hotelName = "Overwritten!";
</script>
Step through both tags. There is only one window box in the middle, and both tags read and write the same slot in it. · interactive

A var declared in the first tag can be read, and overwritten, from the second. This is the same “public noticeboard” collision risk from Lesson 3, shown across whole <script> tags.

Strict Mode Changes The Rule — But Only Sometimes

strict.js
'use strict';
console.log(this); // still window — top-level this is unaffected by strict mode

function greet() {
  console.log(this);
}
greet(); // undefined in strict mode (would be window without 'use strict')
WhereNon-strict modeStrict mode
Top level of a classic scriptthis = windowthis = window (unchanged)
Inside a plain function call, no owner objectthis = windowthis = undefined

'use strict' does not change this at the very top of a script. It is window either way. What it changes is this inside a plain function call (a call with no owner object). Normally that also defaults to window, but strict mode makes it undefined instead. This stops functions from accidentally writing onto the global object.

See It Live — Console & DevTools

  1. Open the browser console

    Any page → DevTools (the debugging tools built into the browser, opened with F12) → Console tab.

  2. Confirm the identity directly

    console
    console.log(this === window); // true
  3. Attach a global variable and check all three routes

    console
    var hotelName = "Grand Palace";
    console.log(hotelName, this.hotelName, window.hotelName); // all three match
  4. Check the universal name

    console
    console.log(globalThis === window); // true, in a browser
  5. Try the strict-mode function case

    Paste the 'use strict' example from above and confirm greet() logs undefined instead of window.

Common Misconceptions

“this always refers to the current function.” At the global level, this refers to the global object. No function is involved yet. The full rules for this inside functions and methods depend on how the function is called, and they are covered in their own lesson.

“Strict mode makes this undefined everywhere, including at the top of the script.” Not true — top-level this stays as the global object in strict mode too. Only a plain function call's this switches to undefined.

“Each <script> tag gets its own separate global scope.” They don't. Every classic <script> tag on the same page shares onewindow object, which is how global variables leak and collide across them.

Interview Questions

What gets created even in a completely empty JS file?

A Global Execution Context (the first container the engine creates to run code), its Variable Environment (its memory), a global object (the one object that holds global names), and a this keyword pointing at that global object.

What is the global object called in a browser vs. in Node.js?

window in a browser, global in Node.js, and self in a Web Worker. globalThis works as a universal name in any environment.

At the top level, what does this equal?

The global object. In a browser classic script, this === window. (In an ES module it is undefined.)

If two separate <script> tags both declare var count, do they conflict?

Yes — both tags share the same window object, so the second declaration silently overwrites the first.

Does 'use strict' change this at the very top of a script?

No — top-level this is still the global object under strict mode. Strict mode only changes this inside a plain function call, making it undefined instead of the global object.

Why was globalThis introduced if window already existed?

Because window only exists in the main browser thread. Code meant to run in Node.js or Web Workers needed one name that works everywhere, and globalThis is that name.

Published by Jainik · Last updated · How these lessons are made

Keep learning

Related series