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.
"use strict";
// Your JavaScript code starts here.
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.
"use strict";
price = 499; // ReferenceError: price is not defined
function calculateTotal() {
"use strict";
total = 100; // ReferenceError
}
calculateTotal();
Without strict mode, assigning to a name that was never declared can create a global variable by mistake. Strict mode blocks this immediately.
"use strict";
message = "Hello"; // ReferenceError
let title = "JavaScript";
const year = 2026;
The delete operator is for deleting object properties. It cannot delete declared variables or function declarations in strict mode.
"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.
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.
"use strict";
// SyntaxError: Duplicate parameter name not allowed
// function add(price, price) {
// return price + price;
// }
function add(price, tax) {
return price + tax;
}
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.
"use strict";
// let oldOctal = 010; // SyntaxError
let modernOctal = 0o10;
console.log(modernOctal); // 8
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.
"use strict";
const person = { name: "Uttam" };
Object.defineProperty(person, "id", {
value: 101,
writable: false
});
person.id = 202; // TypeError
"use strict";
const circle = {
radius: 10,
get area() {
return Math.PI * this.radius * this.radius;
}
};
circle.area = 500; // TypeError
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.
"use strict";
function showThis() {
console.log(this);
}
showThis(); // undefined
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".
<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.
Explore 500+ free tutorials across 20+ languages and frameworks.