Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Strict Mode in JavaScript use strict: Tutorial, Examples, FAQs & Interview Tips

Strict Mode

Strict mode was introduced in ECMAScript 5. It is enabled with the "use strict" directive and tells JavaScript to reject some older, error-prone behavior instead of silently allowing it. This makes bugs easier to find, avoids accidental globals, and encourages code that works better with modern JavaScript features.

Modern JavaScript modules and class bodies are strict by default. Regular scripts and regular functions still need the directive if you want strict behavior there.

Syntax
"use strict";

// Your JavaScript code starts here.

Why Use Strict Mode?

  • It turns accidental global variables into real errors.
  • It prevents duplicate function parameters and unsafe syntax.
  • It makes assignment failures visible instead of silently ignoring them.
  • It makes this easier to reason about in plain function calls.
  • It prepares your code for modules, classes, and modern tooling.

Enabling Strict Mode

Place "use strict" at the beginning of a script or function. It must appear before other executable statements. Comments are allowed before it, but normal code is not.

Script Level

Whole Script
"use strict";

price = 499; // ReferenceError: price is not defined

Function Level

Single Function
function calculateTotal() {
  "use strict";

  total = 100; // ReferenceError
}

calculateTotal();

Undeclared Variables Are Not Allowed

Without strict mode, assigning to a name that was never declared can create a global variable by mistake. Strict mode blocks this immediately.

Accidental Global
"use strict";

message = "Hello"; // ReferenceError

let title = "JavaScript";
const year = 2026;

Deleting Variables or Functions Is Not Allowed

The delete operator is for deleting object properties. It cannot delete declared variables or function declarations in strict mode.

delete
"use strict";

let count = 1;
function showCount() {
  return count;
}

// delete count;     // SyntaxError
// delete showCount; // SyntaxError

const user = { name: "Asha" };
delete user.name; // This is allowed.

Duplicate Parameters Are Not Allowed

In older non-strict JavaScript, a function could accidentally use the same parameter name twice. Strict mode treats this as a syntax error because it makes code confusing.

Parameters
"use strict";

// SyntaxError: Duplicate parameter name not allowed
// function add(price, price) {
//   return price + price;
// }

function add(price, tax) {
  return price + tax;
}

Octal Literals Are Restricted

Legacy octal numbers such as 010 are confusing because they look like decimal numbers. Strict mode rejects the old format. Use the modern 0o prefix for octal values.

Octal
"use strict";

// let oldOctal = 010; // SyntaxError

let modernOctal = 0o10;
console.log(modernOctal); // 8

Assignment Failures Throw Errors

Strict mode throws an error when code tries to write to read-only properties, getter-only properties, or non-extensible objects. Without strict mode, these mistakes may fail silently.

Read-only Properties
"use strict";

const person = { name: "Uttam" };

Object.defineProperty(person, "id", {
  value: 101,
  writable: false
});

person.id = 202; // TypeError
Getter-only Properties
"use strict";

const circle = {
  radius: 10,
  get area() {
    return Math.PI * this.radius * this.radius;
  }
};

circle.area = 500; // TypeError

this in Strict Mode

In a normal function call, strict mode leaves this as undefined. Non-strict mode may replace it with the global object, which can hide bugs.

this
"use strict";

function showThis() {
  console.log(this);
}

showThis(); // undefined

Strict Mode and Modern JavaScript

If you write ES modules, strict mode is already enabled automatically. This means a file loaded with <script type="module"> behaves strictly even without writing "use strict".

Modules
<script type="module" src="app.js"></script>

Because modules and classes are strict by default, most modern projects already benefit from strict rules through bundlers, frameworks, or module-based scripts.

Key Takeaways
  • Strict mode is enabled with the "use strict" directive.
  • It prevents accidental globals and several confusing legacy behaviors.
  • It turns silent assignment failures into visible TypeError or ReferenceError messages.
  • Plain function calls have this as undefined in strict mode.
  • ES modules and classes are strict by default.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.