Sub topic 3
Plain Function Calls: Sloppy vs Strict Mode
This chapter assumes you can write and call functions, know what an object property is, and have seen the 'use strict' directive before. It covers the simplest call form in JavaScript: calling a function by name, with no object in front of it and no new. This is the default binding rule. It applies when no other rule does, and its result depends on whether the called function runs in strict mode.
What counts as a plain call
A plain call is any call of the form fn(). Nothing comes before the function reference, so JavaScript has no object to use as this. Here are some examples:
doWork()(function () { ... })()(an immediately invoked function expression)callback()inside a helper that receivedcallbackas an argument
In each case, the engine sets this to undefined. What the function then sees depends on its mode:
| Mode of the called function | Value of `this` in a plain call |
|---|---|
| Sloppy (non-strict) | `globalThis` (`window` in browsers, `global` in Node) |
| Strict | `undefined` |
In sloppy mode, the engine swaps the missing this for the global object. In strict mode, it leaves this as undefined.
Seeing both modes side by side
Save this as a .js file and run it with node file.js. Node treats a plain .js file as a CommonJS script, which is sloppy unless you opt in to strict mode.
function sloppy() {
return this;
}
function strict() {
'use strict';
return this;
}
console.log(sloppy() === globalThis); // true
console.log(strict()); // undefinedThe 'use strict' directive only has an effect as the first statement of a script or function body. Some code is strict automatically, without any directive:
- ES modules (
.mjsfiles,<script type="module">, or packages with"type": "module") - All code inside a
classbody - Functions created with
new Functioninside strict code. Strictness also passes down to nested functions: if the outer code is strict, every function defined inside it is strict too.
Much modern code is written as modules or classes, so strict default binding is what you will usually run into.
Strictness belongs to the callee
People often get this wrong. The mode that matters is the mode of the function being called. The mode of the code doing the calling makes no difference.
function sloppyFn() {
return this === globalThis;
}
function callFromStrict() {
'use strict';
return sloppyFn(); // the caller is strict, the callee is not
}
const strictFn = (function () {
'use strict';
return function () {
return this;
};
})();
console.log(callFromStrict()); // true: sloppyFn still gets globalThis
console.log(strictFn()); // undefined: strictFn was defined in strict codestrictFn has no directive of its own, but it was defined inside a strict function, so it inherits strict mode from there.
Why strict mode is safer
Sloppy default binding turns a mistake into a silent write to the global object. A common case is forgetting new on a constructor-style function:
function Person(name) {
this.name = name;
}
const p = Person('Ada'); // forgot new
console.log(p); // undefined
console.log(globalThis.name); // 'Ada': leaked onto the global objectThe same code in strict mode fails right away, at the line that caused the problem:
'use strict';
function Person(name) {
this.name = name;
}
try {
Person('Ada');
} catch (err) {
console.log(err instanceof TypeError); // true
console.log(err.message); // e.g. "Cannot set properties of undefined (setting 'name')"
}A TypeError with a stack trace is much easier to debug than a global variable that shows up somewhere else in the program.
A note on primitive this values
Sloppy mode also converts any this value that is not an object. null and undefined become globalThis, and primitives such as 'abc' or 42 are wrapped in their object forms (String, Number). Strict mode passes the value through unchanged. You will mostly see this with call and apply, which a later chapter covers. For plain calls, the rule from the table above is all you need.
Exercises
Exercise
In a Node CommonJS file with no 'use strict' directive, what does this code log, and why? function outer() { 'use strict'; function inner() { return typeof this; } return inner(); } console.log(outer());
Show answer
It logs "undefined". The string comes from typeof applied to the value undefined. The function inner is defined inside the strict function outer, so inner is strict too. A plain call to a strict function leaves this as undefined. The file itself being sloppy does not matter.
Exercise
A module file (strict) imports a helper from an old sloppy script and calls it as helper(). Inside helper, this.config is read. Will it throw a TypeError?
Show answer
No. Strictness belongs to the called function. helper is sloppy code, so its this becomes globalThis, and this.config reads globalThis.config, which is usually undefined. It returns that value quietly and does not throw.