Strict Mode and Undefined 'this'
You'll learn how strict mode changes what this refers to in standalone function calls, making bugs easier to catch.
The Default Behavior Problem
In non-strict mode, when you call a regular function (not as a method, constructor, or with explicit binding), this defaults to the global object (window in browsers, global in Node.js). This is rarely what you want and can lead to accidental global variable creation.
Strict mode changes this behavior to help you catch mistakes earlier.
How Strict Mode Helps
When you enable strict mode with "use strict";, standalone function calls have this set to undefined instead of the global object. This means:
"use strict";
function showThis() {
console.log(this);
}
showThis(); // undefined (in strict mode)
// Without strict mode: window or global object
Why This Matters
This change turns silent bugs into immediate errors. If your function accidentally tries to use this when it shouldn't have one, you'll get a clear error instead of mysterious global object pollution:
"use strict";
function createUser(name) {
this.name = name; // TypeError: Cannot set property 'name' of undefined
}
createUser("Alice"); // Crashes! You probably forgot 'new'
Without strict mode, this would silently create a global name variable—a hard-to-debug problem.
The Mental Model
Think of strict mode as removing the "safety net" that catches your this and redirects it to the global object. Instead of silently doing something unexpected, it lets your code fail fast so you notice the mistake immediately.
Key Takeaway: Strict mode makes standalone function calls have undefined as this, turning potential silent bugs into clear errors that help you catch incorrect function usage.