JavaScript Strict Mode

Strict mode is a way to opt into a restricted variant of JavaScript. It eliminates some silent errors, fixes mistakes that make it difficult for engines to optimize, and prohibits syntax likely to conflict with future versions of JavaScript. Enable it with the directive "use strict".

Enabling Strict Mode

Add "use strict" at the beginning of a script or a function to enable strict mode. It must be the very first statement (only comments and whitespace can precede it). Strict mode applies to the entire script or the individual function.

Enabling Strict Mode
// Script-level strict mode
"use strict";

let x = 10;
console.log(x);

// Without strict mode, this would silently create a global
// y = 20;  // ReferenceError in strict mode!

// Function-level strict mode
function strictFunction() {
  "use strict";
  let a = 1;
  console.log("Strict function:", a);
  // b = 2; // Would throw ReferenceError
}

strictFunction();
console.log("Script is in strict mode");

No Undeclared Variables

In strict mode, assigning a value to an undeclared variable throws a ReferenceError. In non-strict mode, this would silently create a global variable — a common source of bugs.

Undeclared Variables
"use strict";

// Must declare variables
let name = "Alice";
console.log(name);

// This would throw ReferenceError:
// undeclaredVar = "oops";

// Also applies to typos
let counter = 0;
// conter = 1;  // ReferenceError! (typo in name)

// Without strict mode, these would silently create globals
function safeFunction() {
  "use strict";
  let result = 42;
  console.log("Safe result:", result);
}
safeFunction();

No Duplicate Parameters

Strict mode does not allow duplicate parameter names in functions. In non-strict mode, duplicate parameter names are allowed (the last one wins), which is almost always a mistake.

Duplicate Parameters Prevention
"use strict";

// This is fine — unique parameter names
function add(a, b) {
  return a + b;
}
console.log(add(3, 4));

// This would throw SyntaxError in strict mode:
// function bad(a, a, b) { return a + b; }

// In non-strict mode, the second 'a' would override the first
// That's almost never intentional

// Good practice: always use unique names
function calculate(price, tax, discount) {
  let total = price + (price * tax) - discount;
  return total;
}
console.log(calculate(100, 0.1, 5));

Other Strict Mode Restrictions

Strict mode has additional restrictions: no 'with' statement, no octal numeric literals (like 010), no deleting variables or functions, no writing to read-only properties (throws instead of silently failing), and reserved keywords cannot be used as variable names.

Additional Restrictions
"use strict";

// No octal literals
// let oct = 010; // SyntaxError
let oct = 0o10;  // Use 0o prefix for octal (ES6)
console.log(oct);

// Cannot delete variables
let x = 10;
// delete x; // SyntaxError

// Writing to read-only property throws
let obj = {};
Object.defineProperty(obj, "readOnly", {
  value: 42,
  writable: false
});
// obj.readOnly = 100; // TypeError in strict mode
console.log(obj.readOnly);

// Cannot use reserved words as variable names
// let implements = 1; // SyntaxError
// let interface = 2;  // SyntaxError
// let package = 3;    // SyntaxError
console.log("Strict mode restrictions active");

Module-Level and Class Strict Mode

ES6 modules are automatically in strict mode — no need for "use strict". Similarly, the body of a class declaration is always in strict mode. This means modern JavaScript patterns enforce strict mode by default.

Automatic Strict Mode
// In ES modules (.mjs or type="module"), strict mode is automatic
// import/export syntax means strict mode is on

// Classes are always strict
class Animal {
  constructor(name) {
    this.name = name;
    // undeclaredProp = "bad"; // Would throw ReferenceError
  }

  speak() {
    return `${this.name} speaks`;
  }
}

let dog = new Animal("Rex");
console.log(dog.speak());

// In practice, if you use modules, you're already strict
console.log("Modules and classes are always strict");

// Check if we're in strict mode
function isStrict() {
  "use strict";
  return !this;
}
console.log("Is strict?", isStrict());
FeatureNon-StrictStrict Mode
Undeclared variableCreates globalReferenceError
Duplicate parametersLast one winsSyntaxError
Octal literals (010)AllowedSyntaxError
with statementAllowedSyntaxError
delete variableReturns falseSyntaxError
Write to read-onlySilently failsTypeError
this in functionGlobal objectundefined
Reserved words as varsAllowedSyntaxError
📝 Note: Always use strict mode. It catches common mistakes early, prevents dangerous patterns, and prepares your code for future JavaScript versions. If you use ES6 modules or classes, strict mode is already enabled automatically. For scripts, add "use strict" at the top.
Exercise:
What happens when you assign to an undeclared variable in strict mode?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.