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

Function Calls: When 'this' is Global (Non-Strict)

Explore how non-strict mode causes standalone function calls to bind 'this' to the global object.

Function Calls: When 'this' is Global (Non-Strict)

What You'll Learn

You'll discover how calling a standalone function in non-strict mode binds this to the global object (usually window in browsers or global in Node.js).

The Global Fallback Behavior

In non-strict mode, JavaScript has a peculiar default: when you call a function without any object context, this doesn't become undefined — instead, it falls back to the global object. This is legacy behavior from early JavaScript that can lead to unexpected bugs.

Think of it like a lost package delivery: if the address (object context) is missing, JavaScript's old rules say "just deliver it to City Hall" (the global object) instead of rejecting the delivery.

How It Works

When you invoke a function as a standalone call (not as a method, not with new, not with call/apply), and you're not in strict mode, this automatically refers to the global object:

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

showThis();  // Logs: Window {...} (in browsers)
             // or global object in Node.js

This means any properties you accidentally set on this inside such a function become global variables:

function accidentalGlobal() {
  this.oops = "I'm global now!";
}

accidentalGlobal();
console.log(oops);  // "I'm global now!" — Yikes!

Why This Is Dangerous

This behavior makes it easy to pollute the global namespace unintentionally. If you forget to use an object context or new keyword, you might create global variables by accident, leading to hard-to-trace bugs.

Key Takeaway: In non-strict mode, standalone function calls bind this to the global object, creating a silent fallback that can pollute your global namespace — one of many reasons to prefer strict mode.