JavaScript strict mode ("use strict";) puts the engine into a stricter execution mode that turns silent mistakes into loud runtime errors. It prevents accidental global variables, blocks unsafe assignments, and helps JavaScript engines optimize code faster.
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).
You do not need to write "use strict"; inside ES modules (import/export) or inside ES6 class bodies, because JavaScript enables strict mode there automatically. Keep "use strict"; at the top of standalone classic scripts.
"use strict";
function buildOrder(price, quantity) {
const subtotal = price * quantity;
return subtotal;
}
// Attempting to modify a frozen object throws 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));
"use strict"; below other statements: The directive only works if it appears as the very first statement of a script or function body.this inside standalone functions to be window: In strict mode, calling a plain function sets this to undefined instead of leaking the global window object.Yes, every ES module file using import or export and every class block runs in strict mode by default without needing the string directive.
"use strict"; affect browsers that do not support it?All browsers released over the last decade support strict mode, and older engines simply treated "use strict"; as a harmless unused string expression.
No, once enabled for a script or function scope, strict mode applies to the entire script or function and cannot be disabled inside nested blocks.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.