What is strict mode in JavaScript
· Category: JavaScript
Short answer
Strict mode is a restricted variant of JavaScript enabled with "use strict"; that catches common mistakes, prevents unsafe actions, and optimizes engine performance.
How it works
Add the directive at the top of a script or function:
"use strict";
x = 3.14; // ReferenceError: x is not declared
Key changes
- Assigning to undeclared variables throws a
ReferenceError. - Deleting variables, functions, or non-configurable properties throws.
- Duplicate parameter names are forbidden.
thisin standalone functions isundefinedinstead of the global object.- Octal numeric literals are forbidden.
Example
function sum(a, a) { // SyntaxError in strict mode
"use strict";
return a + a;
}
Why it matters
Strict mode makes code more secure and predictable. ES modules are implicitly strict, so modern code often runs in strict mode by default.