What is this inside a plain function call in strict mode versus sloppy mode?
In sloppy mode a plain call gets the global object as this. In strict mode it gets undefined. Strict mode also stops call from boxing a primitive this into an object.
Explain it simply
When there is no object before the dot and no call, apply or bind, this falls back to a default. Sloppy (the old, forgiving mode) picks the global object, which makes it easy to read or write global variables by accident. Strict mode picks undefined, so a mistake fails loudly instead of silently touching globals.
You get strict mode with "use strict", inside class bodies and inside ES modules, so most modern code already runs in it.
Strict mode also changes primitives: fn.call(5) gives this as the number 5, while sloppy mode wraps it as a Number object.
Example
function strictFn() {
"use strict";
return this;
}
console.log(String(strictFn())); // Output: undefined
console.log(typeof strictFn.call(5)); // Output: number
function sloppyFn() {
return this;
}
console.log(typeof sloppyFn.call(5)); // Output: objectA plain call to sloppyFn() would return the global object, globalThis.
What to say in the interview
"A plain call has no receiver, so this is the global object in sloppy mode and undefined in strict mode. Classes and modules are strict by default, which is why a detached class method throws a TypeError instead of quietly using globals."
Common mistakes
- Saying this is always the window. That is only true in sloppy mode, browser, plain calls.