How Strict Mode Changes JavaScript Runtime Evaluation
JavaScript’s strict mode, invoked by adding
"use strict"; at the top of a script or function, modifies
the language’s runtime evaluation semantics to make code more robust,
secure, and performant. Instead of failing silently when encountering
problematic patterns, the JavaScript engine alters variable scoping,
function execution context, property assignments, and evaluation
mechanics to actively throw runtime exceptions and prevent error-prone
behaviors.
Elimination of Silent Assignment Failures
In non-strict (sloppy) mode, certain illegal operations fail quietly
without stopping execution. Strict mode converts these silent failures
into explicit runtime TypeError exceptions:
- Undeclared Variables: Assigning a value to an
undeclared variable throws a
ReferenceErrorinstead of implicitly creating a global variable. - Non-Writable Properties: Attempting to assign a
value to a non-writable global variable (such as
undefinedorNaN), a read-only property, or an accessor-only (getter-only) property throws aTypeError. - Non-Extensible Objects: Attempting to add new
properties to an object configured with
Object.preventExtensions(),Object.seal(), orObject.freeze()throws aTypeError. - Deleting Non-Configurable Properties: Attempting to
use the
deleteoperator on non-configurable object properties throws aTypeErrorinstead of returningfalse.
Contextual this
Value Changes
Strict mode changes how the default this context is
evaluated during standard function execution:
- In sloppy mode, invoking a standalone function without a specified
context automatically binds
thisto the global object (windowin browsers orglobalin Node.js). - In strict mode,
thisremainsundefinedif it is not explicitly bound via invocation context,call(),apply(), orbind(). - When primitive values (such as strings, booleans, or numbers) are
passed as
thisusingcallorapply, strict mode preserves the raw primitive rather than automatically boxing it into anObjectinstance.
eval Scope Isolation
Strict mode significantly alters how the eval() function
evaluates code at runtime:
- In sloppy mode, code executed via
eval()can introduce new variables and functions directly into the enclosing lexical scope. - In strict mode,
eval()creates its own private lexical scope. Variables and functions declared inside the evaluated string exist only during the execution of that specificeval()statement and do not leak into the containing scope.
Decoupling of the
arguments Object
Strict mode separates the synchronization between named function
parameters and the indexed arguments object:
- In sloppy mode, modifying an element within
arguments[i]dynamically updates the corresponding named parameter in the function signature, and vice versa. - In strict mode,
argumentsacts as a static snapshot of the arguments passed during invocation. Changing a named parameter does not modify the corresponding value inarguments, nor does modifyingargumentsalter the named parameter. - Accessing
arguments.calleeorarguments.calleris completely restricted and throws aTypeErrorduring evaluation.
Syntax and Variable Restrictions
Strict mode enforces immediate runtime errors on patterns that make static analysis and optimization difficult for the JavaScript engine:
- Duplicate Parameter Names: Declaring a function
with identical parameter names (for example,
function sum(a, a, b)) throws a syntax error prior to execution. - The
withStatement: Thewithstatement is entirely banned because it makes variable resolution ambiguous at runtime; encountering it triggers an immediate syntax error. - Legacy Octal Literals: Prefixed numeric literals
using a leading zero (e.g.,
0644) are forbidden to avoid ambiguity, requiring explicit octal prefixes instead (e.g.,0o644).