Strict Mode
Master this topic with zero to advance depth.
Expert Answer & Key Takeaways
Mastering Strict Mode is essential for high-fidelity technical architecture and senior engineering roles in 2026.
JavaScript Use Strict
The
"use strict"; directive was introduced in ECMAScript version 5. It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.
The purpose of "use strict" is to indicate that the code should be executed in "strict mode".1. What is Strict Mode?
Strict mode makes it easier to write "secure" JavaScript. It changes previously accepted "bad syntax" into real errors.
As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.
2. Declaring Strict Mode
Strict mode is declared by adding
"use strict"; to the beginning of a script or a function.
Global Scope:"use strict";
x = 3.14; // This will cause an error (x is not defined)Local Scope (inside a function):
x = 3.14; // This will not cause an error.
myFunction();
function myFunction() {
"use strict";
y = 3.14; // This will cause an error
}3. Benefits of Strict Mode
- Captures Silent Errors: Converts common coding mistakes into actual errors (like assigning to a non-extensible object).
- Fixes Mistakes: Solves errors that make it difficult for JavaScript engines to perform optimizations.
- Prohibits Unsafe Syntax: Disallows some syntax likely to be defined in future versions of ECMAScript (like
implements,interface,package).
4. Not Allowed in Strict Mode
- Using a variable without declaring it:
x = 3.14; - Deleting a variable or function:
delete x; - Duplicating a parameter name:
function x(p1, p1) {}; - Octal numeric literals are not allowed:
let x = 010; - Writing to a read-only property.
[!IMPORTANT] Classes and Modules in modern 2026 JavaScript are strictly always in strict mode by default, without needing the directive.
Top Interview Questions
?Interview Question
Q:Why use 'use strict' in JavaScript?
A:
It helps catch silent errors, prevents the accidental creation of global variables, and forbids unsafe syntax, leading to cleaner and more performant code.
?Interview Question
Q:Are ES6 Classes always in strict mode?
A:
Yes. Code inside ES6 classes and ES modules is automatically executed in strict mode by default.
?Interview Question
Q:What happens if you assign a value to an undeclared variable in strict mode?
A:
It throws a ReferenceError. In non-strict mode, it would silently create a new property on the global object (window).
Course4All Engineering Team
Verified ExpertSenior Full-Stack Engineers & V8 Experts
Our JavaScript and engine-level content is developed by a collective of senior engineers focused on high-performance web architecture and 2026 standards.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.