Course contentsShow
← JavaScript
Lesson 541 of 2,72314. The 'this' KeywordFree lesson

The Global 'this' Context

How 'this' behaves in the global scope: pointing to window (browser) or global (Node) in non-strict mode.

The Global 'this' Context

You'll learn: How this behaves when referenced in the global scope—the outermost level of your code.

What Happens at the Top Level

When you reference this outside of any function or object, you're in the global context. Here, this points to the global object itself. Think of it like being in the lobby of a building—when you're not inside any specific room (function), you're in the shared common area.

In a web browser, the global object is window. In Node.js, it's called global. So this at the global level literally refers to the environment's global object:

console.log(this); // browser: Window {...}, Node: Object [global] {...}

This means global variables declared with var (or without any keyword) become properties of the global object:

var myVar = 42;
console.log(this.myVar); // 42 (in non-strict mode)
console.log(window.myVar); // 42 (in browser)

The Strict Mode Exception

In strict mode ('use strict';), the rules change slightly. While this still refers to the global object at the global scope itself, functions called in the global context won't automatically bind this to the global object—but that's a binding rule you'll explore later. At the pure global level, though, this remains the global object even in strict mode.

Why This Matters

Understanding that this defaults to the global object helps you avoid accidental pollution of the global namespace and explains why variables can sometimes be accessed through window or global.

Key Takeaway: In the global scope, this points to the global object: window in browsers, global in Node.js (non-strict mode).