Strict mode, enabled with a single "use strict" directive (and on by default in ES modules and classes), changes JavaScript’s behavior to catch more mistakes and eliminate several confusing legacy quirks.
"use strict";
// Without strict mode, this silently creates a global variable
// undeclaredVar = 5; // ReferenceError in strict mode - the safety net
function example() {
"use strict";
// 'this' inside a plain function call is undefined, not the global object
console.log(this); // undefined
}
example();
// Assigning to a read-only property fails LOUDLY instead of silently
const obj = Object.freeze({ x: 1 });
// obj.x = 2; // TypeError in strict mode; silently ignored without it
Where It’s Already On By Default
ES modules (import/export) and class bodies are automatically strict — you don’t need the directive there. It only matters to add explicitly in plain script files or CommonJS modules that predate ES modules.