Lesson

JS Strict Mode

10 min reading
Free Course

JavaScript Strict Mode: Catching Silent Bugs Early

JavaScript strict mode, switched on with the "use strict"; directive, makes the engine treat common mistakes as real errors instead of ignoring them. It prevents accidental global variables, blocks unsafe assignments, and removes a few confusing language behaviors. This guide explains what changes under strict mode, where it is enabled automatically, and the mistakes that make the directive silently do nothing.

What Changes Under Strict Mode

In classic sloppy mode, assigning a typo like usreName = "Ada" silently creates a global property on window. Under strict mode, that line immediately throws a ReferenceError so you catch the bug during testing.

Strict mode also throws a TypeError when you assign to read-only properties, delete plain variables, or pass duplicate parameter names like function add(a, a).

Where Strict Mode Is Automatic

You do not need to write "use strict"; in these places, because strict mode is already on:

  • ES modules (files that use import or export, or are loaded with <script type="module">).

  • Class bodies, including methods and constructors.

Standalone classic scripts are different. Add the directive at the top of the file, or at the top of a function if you only want strict rules inside it.

// Function-level strict mode
function safeSum(a, b) {
  "use strict";
  return a + b;
}

A Practical Strict Mode Example

"use strict";

function buildOrder(price, quantity) {
  const subtotal = price * quantity;
  return subtotal;
}

// Modifying a frozen object throws a TypeError in strict mode
const settings = Object.freeze({ currency: "USD" });
try {
  settings.currency = "EUR";
} catch (err) {
  console.log("Caught strict mode error:", err.name);
}

console.log("Order total:", buildOrder(15, 3));

What You Should See

Caught strict mode error: TypeError
Order total: 45

To confirm strict mode is really active, delete the "use strict"; line and run it again. The TypeError disappears, and the assignment fails silently. You can also test with an undeclared variable:

"use strict";
undeclaredValue = 10; // ReferenceError: undeclaredValue is not defined

Without the directive, the same line runs without complaint and creates a global.

How to Add Strict Mode to an Existing Project

Turning it on for old code can expose hidden bugs, so go step by step:

  1. Start with one file or one function, not the whole codebase.

  2. Open the browser console or run the script in Node.js and look for new errors.

  3. Fix each error at its source. Typically it is an undeclared variable, a duplicate parameter, or code that relied on this being window.

  4. Consider an ESLint setup that flags these mistakes before you run the code. The strict rule can enforce the directive, and ESLint's recommended rules already catch undeclared variables.

  5. When a project moves to ES modules, strict mode comes with the change.

Common Strict Mode Mistakes

  • Placing "use strict"; after other statements. The directive counts only when it is the first statement of a script or function body. Comments and blank lines before it are fine. Any real code before it turns it into an unused string, with no warning.

  • Expecting this to be window in plain functions. In strict mode, calling a function normally sets this to undefined. Code like function show() { console.log(this.name); } then throws a TypeError. Use explicit parameters or bind()/arrow functions instead.

  • Mixing strict and non-strict files in a bundle. If a build tool concatenates scripts, one file's "use strict"; at the very top can make the whole bundle strict, including code that was never written for it. Wrap each file in a function or use ES modules to keep the boundaries clear.

  • Putting the directive in a function with non-simple parameters. A function that uses default values, destructuring, or rest parameters cannot contain its own "use strict"; That throws a SyntaxError. Put the directive at the top of the file instead.

For related lessons on KoderSolution, see JavaScript variables and JavaScript modules. Strict mode also changes how this behaves.

Conclusion

JavaScript strict mode turns silent mistakes into errors you can see and fix. Use ES modules where you can, since they are strict by default. In classic scripts, put "use strict"; first, and add it to older projects gradually. Next, read how JavaScript variables are declared and scoped.

Frequently Asked Questions

Do ES modules and React components run in strict mode automatically?

ES modules and class bodies do. A React function component is not strict by itself, but it almost always lives in an ES module, so it runs in strict mode anyway.

Does "use strict" break older browsers?

No. Browsers that don't understand it treat it as an ordinary string expression and ignore it. Strict mode is supported in all current browsers.

Can I turn strict mode off partway through a file?

No. Once it is on for a script or function, it stays on for everything inside it, including nested functions. You can start strict mode in a function inside a non-strict script, but you cannot switch it off later.

Does strict mode make my code faster?

Possibly, since removing some dynamic behavior can help engines. Treat that as a side effect, though, not a reason to use it. The real benefit is catching bugs earlier.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
JavaScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum