JavaScript Strict Mode Use Strict Explained

Learn what JavaScript strict mode is, how to enable it with 'use strict', and how it catches silent mistakes that would otherwise go unnoticed.

5 min read

JavaScript strict mode is a stricter way to parse and run JavaScript. It turns several silent mistakes into errors and changes a few confusing language behaviors.

You enable it in classic scripts with a directive string: "use strict".

How to Enable Strict Mode

Put the directive at the top of a script or function body, before any other statement. That placement matters because directive strings only count before normal code starts.

javascriptjavascript
"use strict";
 
let score = 100;
console.log(score);

Comments can appear above the directive. A real statement cannot, because then the string is no longer treated as a directive.

You can also enable it for one function:

javascriptjavascript
function calculateTotal(items) {
  "use strict";
  return items.length;
}

Only that function body is strict. Other functions are strict only if their own code is strict.

What Strict Mode Catches

Strict mode is useful because it makes common mistakes fail loudly.

javascriptjavascript
"use strict";
 
score = 100;

This throws a ReferenceError because score was never declared. In non-strict scripts, that mistake can create a global variable.

Strict mode also catches writes to read-only properties:

javascriptjavascript
"use strict";
 
const user = {};
Object.defineProperty(user, "id", { value: 1, writable: false });
user.id = 2;

The assignment throws a TypeError instead of failing silently.

Strict Mode Changes this

In a plain function call, strict mode leaves this as undefined.

javascriptjavascript
function showThis() {
  "use strict";
  console.log(this);
}
 
showThis();

The output is undefined. In non-strict browser scripts, a plain function call can use the global object instead, which often causes accidental global mutations.

Where Strict Mode Is Automatic

Modern JavaScript turns on strict mode automatically in some places:

Code LocationStrict by Default
JavaScript moduleYes
Class bodyYes
Classic script fileNo
Function bodyOnly with directive

If you are writing modules, you do not need to add "use strict". If you are writing an older classic script, adding the directive is still useful.

Strict Mode Scope

Strict mode is lexical. That means it depends on where code is written, not who calls it.

javascriptjavascript
"use strict";
 
function strictFunction() {
  return "strict";
}

This function is strict because it is written inside a strict script. But a separate non-strict function does not become strict just because strict code calls it.

There is also no directive that turns strict mode off inside a strict scope.

Should You Use Strict Mode?

For new classic script files, yes. It catches real mistakes and makes behavior easier to reason about.

For modules and classes, it is already active. For old legacy code, enable it carefully and test, because strict mode can expose mistakes that were previously ignored.

For broader consistency, see the JavaScript code style guide. You can also practice small strict-mode examples in Chrome DevTools.

Rune AI

Rune AI

Key Insights

  • JavaScript strict mode is enabled with a use strict directive.
  • The directive must appear before other statements in a script or function body.
  • Modules and class bodies are strict automatically.
  • Strict mode turns several silent failures into errors.
  • Strictness is lexical, so calling a non-strict function from strict code does not make that function strict.
RunePowered by Rune AI

Frequently Asked Questions

Do modules need use strict?

No. Modules are strict by default.

Can strict mode be disabled inside strict code?

No. Once a script or function body is strict, that scope cannot opt out.

Does strict mode catch every bug?

No. It catches specific language mistakes, not all logic errors.

Conclusion

Strict mode makes JavaScript less forgiving in useful ways. Use it in classic scripts, and remember that modules and class bodies already run in strict mode.