More JavaScript answers

by Danny
Published 3 min read

"use strict" turns silent JavaScript mistakes into errors. ES modules and class bodies are strict automatically, so you mainly need it in classic scripts and CommonJS files.

The short answer

#

"use strict" switches on strict mode, a stricter version of JavaScript that turns several silent mistakes into errors. You do not need it in ES modules or inside class bodies, because they are always strict. You still benefit from it in classic script files and in CommonJS files in Node.js.


_10
'use strict';
_10
_10
function setTotal() {
_10
totl = 10; // ReferenceError: totl is not defined
_10
}

Why it happens

#

Early JavaScript was designed to keep running whatever happened, so many mistakes failed silently. A typo in a variable name created a new global. Writing to a read-only property did nothing. Strict mode, added in ES5, keeps the language the same for correct code but makes these cases throw.

To avoid breaking existing websites, strict mode had to be opt-in, which is why it is a string directive rather than new syntax. Older engines simply see a string and ignore it.

The main changes are:

  • Assigning to an undeclared variable throws a ReferenceError instead of creating a global.
  • this is undefined in a plain function call, rather than the global object.
  • Writing to a read-only property, or adding a property to a frozen object, throws a TypeError.
  • Duplicate parameter names, the with statement and legacy octal literals such as 010 are syntax errors.
  • Deleting a variable or a non-configurable property throws.
  • eval cannot create variables in the surrounding scope.
  • Words such as implements, interface, private, protected, public and static are reserved.

How to fix it

#

In ES modules, do nothing

Any file loaded as a module is strict automatically. That includes script tags with type="module", files with import or export in Node.js when they are treated as ESM (for example a .mjs file, or a .js file in a package with "type": "module"), and the output of most modern bundlers.


_10
// app.mjs: already strict, no directive needed
_10
export function total(items) {
_10
count = items.length; // ReferenceError
_10
}

In classes, do nothing

Everything inside a class body is strict, even in a classic script.


_10
class Basket {
_10
add() {
_10
return this; // undefined if called without a receiver
_10
}
_10
}

In classic scripts and CommonJS, add it at the top

Ordinary script tags without type="module", CommonJS files using require, and .cjs files are not strict by default. Put the directive as the very first statement of the file.


_10
'use strict';
_10
_10
const express = require('express');

It can also go at the top of a single function, which makes only that function strict.

Let your tools handle it

TypeScript's alwaysStrict option, which strict turns on, emits "use strict" in non-module output and parses files in strict mode. ESLint's strict rule can check for the directive in CommonJS code.

Common pitfalls

#
  • Putting it after other code. The directive only works as the first statement of a file or function. Comments may come before it, but any other statement makes it a plain string that does nothing.
  • Concatenating files. Joining a strict file to a non-strict one can make the whole bundle strict, or none of it, depending on which comes first. Bundlers handle this; hand-rolled concatenation does not.
  • Using it in a function with default or destructured parameters. A "use strict" directive inside a function with non-simple parameters is a SyntaxError. Make the whole file strict instead.
  • Relying on this being the global object. Code that calls a function without a receiver and expects this to be window breaks in strict mode. Use globalThis when you mean the global object.
  • Adding it to modules. It is harmless, but redundant. Leave it out so readers are not confused about which kind of file they are reading.
  • Expecting it to catch type errors. Strict mode is not type checking. It will not stop you passing a string where a number is expected.
#

Was this resource helpful?