Strict mode

Strict mode is a set of rules that turns silent mistakes into errors. A file turns it on with 'use strict'; as its first statement, a single function can turn it on for its own body, and every module and every class body is strict without a directive. The opposite state has no name in the language and is called sloppy mode in the documentation.

The rules that matter at this point of the course are three: a plain call gets undefined as this instead of the global object, a refused delete throws instead of answering false, and an assignment to a name that was never declared throws a ReferenceError instead of creating a global. The programs below run the same code in both modes, which takes two kinds of file: a .mjs module is strict, a .cjs script is not unless it asks.

Sloppy mode, the old default

A CommonJS script with no directive runs in sloppy mode. The program below calls a function with no object and compares this with the global object, then deletes a property and a property that does not exist. All three operations succeed, and none of them reports anything.

function plainCall() { return this; } console.log('this in a plain call:', plainCall() === globalThis); const options = { debug: true }; console.log('delete a property:', delete options.debug, 'debug' in options); console.log('delete a missing property:', delete options.port);

node sloppy.cjs

this in a plain call: true
delete a property: true false
delete a missing property: true

The first line is the reason this was confused for the global object for years: in this mode a function that is called alone really does get the global object, so a write to this.name changes a global. The two delete lines answer true because a delete that removes nothing is still a success.

'use strict' and modules

The directive is the string 'use strict' as the first statement of a file or of a function body. A function that has it is strict from its opening brace, and the rest of the file keeps the mode it had. The program below defines two functions that differ in one line and calls both with no object.

function sloppy() { return this; } function strict() { 'use strict'; return this; } console.log('sloppy function:', sloppy() === globalThis); console.log('strict function:', strict());

node directive.cjs

sloppy function: true
strict function: undefined

The same call gave two answers because the functions run under two modes. A module needs no directive at all: the .mjs extension, and a <script type="module"> tag in a browser, put the whole file in strict mode. New code is mostly written as modules, which is why the directive is rare in it.

What changes

The program below is a module, so all of it is strict. It reads this in a plain call, deletes a property that was defined as non-configurable, and assigns to a name that has no declaration. The first answer is undefined and the other two are errors, each caught so that the output shows all three.

function plainCall() { return this; } console.log('this in a plain call:', plainCall()); const locked = Object.defineProperty({}, 'id', { value: 7, configurable: false, }); try { delete locked.id; } catch (error) { console.log(error.name + ': ' + error.message); } try { leaked = 42; } catch (error) { console.log(error.name + ': ' + error.message); }

node strict.mjs

this in a plain call: undefined
TypeError: Cannot delete property 'id' of #<Object>
ReferenceError: leaked is not defined

The assignment on the nineteenth line is deliberately wrong: it is the line this section is about, and the message is the one the runtime throws. In sloppy mode the same line would create a global called leaked and print nothing, and the typo would live on in the program as a variable nobody declared.

Why strict mode exists

The mode arrived with ECMAScript 5 as a way to fix the parts of the language that could not be changed without breaking the web. Creating a global by assignment, reading this as the global object and deleting a variable were all behaviour that old pages depended on, so the language kept them in scripts and switched them off in the new mode. Every module and every class is strict, which means new code gets the rules without a directive.

A file that mixes the two is the usual source of confusion: a require call loads a sloppy script while the importing file is a strict module, and a function inside the script keeps its own mode. Not every program of this course is a module: a file run as node name.js is a CommonJS script, and so sloppy, unless the package.json of its folder says "type": "module" or the file has an import or export statement. The three programs above name their mode with the .cjs and .mjs extensions, so it does not depend on the folder.

Exercises

  1. Rename sloppy.cjs to sloppy.mjs and run it. Which two lines of the output change their answer?
  2. Add 'use strict'; as the first line of directive.cjs and run it. Which line changes, and why does the directive at the top of a file do more than the one inside a function?
  3. Remove the try block around the assignment in strict.mjs. How many lines does the program print, and what is the exit status?
  4. Write a sloppy script that assigns to an undeclared name and prints typeof globalThis.leaked, then run the same line in a module. Compare the two answers.
  5. Write a function that deletes its own parameter and call it from a strict module. Which error arrives, and why is it a parse error rather than a TypeError?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How does a file turn on strict mode?

    Answer

    With the statement "use strict" as its first statement

    The directive is a string statement at the start of a file or a function body, and a module has it implicitly.

  2. What is this in a plain call of a strict function?

    Answer

    undefined

    Strict mode stops the substitution of the global object, so a call with no object leaves this undefined.

  3. What does this program print? function show() { "use strict"; return this; } console.log(show());

    Answer

    undefined

    The directive makes the function body strict, so the plain call supplies no this and it stays undefined.

  4. What does an assignment to an undeclared name do in a module?

    Answer

    It throws a ReferenceError

    A module is strict, and strict mode refuses to create a global by assignment; the error names the missing binding.

  5. What does this module print? try { leaked = 42; } catch (error) { console.log(error.name); }

    Answer

    ReferenceError

    The file is a module, so the undeclared assignment throws and the error is a ReferenceError.

  6. What does delete options.debug return when the property exists and is configurable?

    Answer

    true, and the property is gone

    A successful delete answers true, and the in check in the same line reports that the key has gone.

  7. What does a refused delete do in strict mode?

    Answer

    It throws a TypeError

    Sloppy mode answers false and carries on, while strict mode turns the silent answer into a TypeError.

  8. Which files are strict without a directive?

    Answer

    Modules and class bodies

    A module and a class body are strict by definition, while a classic script keeps sloppy mode unless it asks.

  9. What does this program print? function strict() { "use strict"; return this; } console.log(strict() === undefined);

    Answer

    true

    The strict function returns undefined for this, and the comparison with undefined is true.

  10. Why does a sloppy script that assigns to a typo keep running?

    Answer

    Because the assignment creates a global variable instead of throwing

    The implicit global is the old behaviour the strict mode removed, and it is why the mistake can survive for a long time.

The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev