Strict mode is a restricted variant of JavaScript that turns silent mistakes into thrown errors and disables unsafe language features. It was introduced in ES5 and is the default in ES modules and modern class bodies.

Enabling Strict Mode

Place "use strict"; as the first statement in a script:

  "use strict";

x = 10; // ReferenceError: x is not defined
  

The string must be exactly "use strict" or 'use strict' — no extra spaces or concatenation tricks.

For a Single Function

  function strictFunction() {
    "use strict";
    // Only this function and nested functions run in strict mode
}

function looseFunction() {
    y = 20; // Creates global variable (non-strict)
}
  

Automatic Strict Mode

These contexts are always strict — no directive needed:

  • ES modules (<script type="module"> or import/export)
  • Class constructors and class methods
  • async function bodies (when the enclosing scope is strict)
  // module.js — automatically strict
export function add(a, b) {
    return a + b;
}
  

What Strict Mode Changes

1. No Accidental Globals

Without strict mode, assigning to an undeclared variable creates a property on the global object:

  "use strict";
mistypedVariable = 42; // ReferenceError

// Correct:
let mistypedVariable = 42;
  

This catches typos like lenght instead of length immediately.

2. Assignment to Read-Only Properties Fails

  "use strict";
NaN = 1;        // TypeError
undefined = 1;  // TypeError
Object.defineProperty({}, "x", { writable: false, value: 1 }).x = 2; // TypeError
  

3. Duplicate Parameter Names Are Syntax Errors

  "use strict";
function sum(a, a) { // SyntaxError in strict mode
    return a + a;
}
  

4. Delete on Non-Configurable Properties Throws

  "use strict";
delete Object.prototype; // TypeError
let obj = {};
Object.defineProperty(obj, "x", { configurable: false, value: 1 });
delete obj.x; // TypeError
  

5. eval Does Not Leak Variables

  "use strict";
eval("var secret = 42");
console.log(typeof secret); // 'undefined' — scoped to eval
  

6. this Is undefined in Plain Functions

  "use strict";
function showThis() {
    console.log(this); // undefined (not window)
}
showThis();
  

7. Octal Literals Are Prohibited

  "use strict";
let n = 0123; // SyntaxError — use 0o123 instead
  

8. with Statement Is Forbidden

  "use strict";
with (Math) { x = cos(0); } // SyntaxError
  

Practical Example

  "use strict";

function divide(a, b) {
    if (typeof b !== "number" || b === 0) {
        throw new Error("Division requires a non-zero number");
    }
    return a / b;
}

console.log(divide(10, 2));  // 5
divide(10, 0);               // Error: Division requires a non-zero number
  

Strict Mode in Modules and Bundlers

Modern projects use bundlers (Webpack, Vite, esbuild) that output ES modules. If your source files use import/export, strict mode is automatic:

  // utils.js — strict by default
export function clamp(value, min, max) {
    return Math.min(Math.max(value, min), max);
}
  

Legacy script tags without type="module" need the explicit directive:

  <script>
    "use strict";
    // your code
</script>
  

Best Practices

  • Always use strict mode in new projects — via modules or the directive at file top.
  • Migrate legacy code incrementally — add "use strict" function-by-function if a full-file migration breaks things.
  • Combine with ESLint — rules like no-undef and strict catch issues before runtime.
  • Never disable strict mode to “fix” errors — the errors reveal real bugs.

Troubleshooting

Error in Strict Mode Likely Cause Fix
ReferenceError: x is not defined Missing let/const/var Declare the variable
TypeError: Cannot assign to read only property Writing to frozen/immutable value Check object state before assignment
SyntaxError: Duplicate parameter name Same name twice in function params Rename one parameter
this is undefined Method called without object context Use .bind(), arrow function, or call as obj.method()

Strict Mode vs Sloppy Mode Summary

Behavior Sloppy Mode Strict Mode
Undeclared assignment Creates global ReferenceError
this in plain function Global object undefined
Duplicate params Allowed SyntaxError
Delete non-configurable prop Silently fails TypeError
Octal literals (0123) Allowed SyntaxError

Strict mode makes JavaScript more predictable and is considered mandatory for all new code. Combined with linting and TypeScript, it forms the foundation of reliable JavaScript development.