What Happens When You Use "use strict" Mode in JavaScript
Using "use strict" activates Strict Mode, which enforces safer coding practices by throwing errors for silent failures, preventing accidental global variables, and restricting unsafe language features.
The lydiahallie/javascript-questions repository documents how this directive transforms JavaScript execution. According to the source code analysis of the project's README.md, strict mode applies tighter semantic rules that help catch common mistakes early and prevent unsafe actions that would otherwise fail silently.
How to Enable Strict Mode
You can activate strict mode by placing the "use strict" directive at the beginning of a script or function. When placed at the top of a file, it applies to the entire script. When placed inside a function, it applies only to that function's scope.
// Global strict mode for entire script
"use strict";
function myFunction() {
// This function runs in strict mode
}
function nonStrict() {
// This function does not run in strict mode
}
Key Behavioral Changes in "use strict" Mode
Undeclared Variables Throw ReferenceError
Without strict mode, assigning a value to an undeclared variable creates a new property on the global object. In strict mode, this throws a ReferenceError immediately.
As documented in the repository's README.md (lines 340-350), this prevents accidental global variable pollution. The code example from Question 20 demonstrates that x = 10 throws ReferenceError: x is not defined rather than creating a global x.
"use strict";
function assignVariable() {
x = 10; // ReferenceError: x is not defined
}
assignVariable();
Silent Failures Become Thrown Errors
Certain operations that would silently fail in non-strict mode now throw exceptions. This includes:
- Assigning to non-writable properties
- Deleting undeletable properties
- Modifying frozen objects
"use strict";
const obj = {};
Object.defineProperty(obj, 'readOnly', {
value: 42,
writable: false
});
obj.readOnly = 100; // TypeError: Cannot assign to read only property
this Binding Remains Undefined
In regular functions invoked without context, this no longer defaults to the global object (window in browsers, global in Node.js). Instead, this remains undefined, preventing accidental global mutation.
"use strict";
function showThis() {
console.log(this); // undefined, not Window or global
}
showThis();
Restrictions on eval and arguments
Strict mode imposes security and scoping restrictions on eval and the arguments object:
- Variables created inside
evaldo not leak into the surrounding scope - The
argumentsobject behaves more predictably and does not alias named parameters
"use strict";
eval("var secret = 42;");
console.log(typeof secret); // "undefined" - variable is confined to eval
function testArgs(a) {
arguments[0] = 10;
return a; // Still returns original value, not 10
}
Duplicate Parameter Names Are Syntax Errors
Function definitions cannot contain multiple parameters with the same name. This catches logical errors that might otherwise cause confusing behavior.
"use strict";
// SyntaxError: Duplicate parameter name not allowed in this context
function duplicate(a, a) {
return a;
}
Practical Code Examples from the Repository
The lydiahallie/javascript-questions repository provides concrete demonstrations of strict mode behavior. The following examples align with the documentation found in the main README.md and its translations (uk-UA/README.md, de-DE/README.md).
Preventing Global Variable Leaks:
// Without strict mode: creates global variable
// With strict mode: throws ReferenceError
function accidentalGlobal() {
"use strict";
undeclaredVar = "I leak"; // ReferenceError
}
Securing this Context:
"use strict";
function ConstructorCheck() {
// Without strict mode, calling ConstructorCheck() would
// pollute global scope. With strict mode, 'this' is undefined.
this.value = 10; // TypeError: Cannot set property 'value' of undefined
}
// Accidental function call instead of constructor
ConstructorCheck();
Summary
"use strict"enables Strict Mode, a restricted variant of JavaScript that eliminates silent errors and unsafe features.- Undeclared variables throw
ReferenceErrorinstead of creating global properties, preventing namespace pollution. - Silent failures (assigning to read-only properties, deleting undeletable properties) throw explicit errors.
thisbinding remainsundefinedin regular functions rather than defaulting to the global object.- Security restrictions prevent
evalfrom leaking variables and makeargumentsbehave predictably. - Duplicate parameter names in functions trigger syntax errors rather than confusing runtime behavior.
Frequently Asked Questions
What is the difference between global strict mode and function-level strict mode?
Global strict mode applies to an entire script when "use strict" appears at the top of the file, affecting all code in that file. Function-level strict mode applies only within the function where the directive appears, allowing you to incrementally adopt strict mode in legacy codebases without breaking existing non-strict scripts.
Does strict mode affect performance in JavaScript?
Strict mode can enable engine optimizations that are impossible in non-strict mode. Because strict mode eliminates dynamic scoping behaviors like eval leaking variables and prevents this from being boxed into an object, JavaScript engines can optimize variable lookups and property access more aggressively. However, the primary benefit remains code safety and maintainability rather than raw speed.
Can I use strict mode in ES6 modules and classes automatically?
Yes, ES6 modules and class declarations automatically enable strict mode without requiring the explicit "use strict" directive. All code inside ES6 modules (import/export syntax) and all code inside class bodies runs in strict mode by default. This design decision was made because strict mode represents the future direction of the language, and modules/classes are modern features that align with that direction.
Why does strict mode prevent duplicate parameter names in functions?
Strict mode treats duplicate parameter names as a syntax error because they create ambiguity about which parameter is being referenced within the function body. In non-strict mode, duplicate parameters are allowed but only the last occurrence is accessible, which can lead to confusing bugs where developers expect to access earlier parameters. By throwing a syntax error at parse time, strict mode catches this logical error before the code executes.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →