Strict mode can be applied to entire scripts or individual functions by
including 'use strict' before any other statements.
// script.js
'use strict';
const x = "Hello from a strict mode script";
// other.js
function strict() {
'use strict';
const x = 'Hello from a strict mode function';
}
This enforces more strict parsing and error handling of JavaScript code, as described below.
Strict mode makes it impossible to accidentally create global variables due to mistyped variable names. Assignments, which would accidentally create global variables, instead throw an error in strict mode:
'use strict';
myVariable = 42;
// The above line will throw a ReferenceError, assuming no global
// variable named myVariable has been declared previously
Strict mode changes some previously-accepted mistakes into errors. These include:
'use strict';
let undefined = 5; // TypeError (non-writable global)
let obj = {};
Object.defineProperty(obj1, 'x', { value: 1, writable: false });
obj.x = 2; // TypeError (non-writable property);
delete Object.prototype; // TypeError (undeletable property)
delete something; // SyntaxError (plain name)
const sum (a, b, b) { // SyntaxError (duplicated argument name)
return a + b + b;
}
const x = 012; // SyntaxError (0-prefixed octal literal)
false.true = 10; // TypeError (property on primitive)
eval
Strict mode makes eval more transparent by preventing the
introduction of new variables in the surrounding scope. In strict mode,
eval creates variables only for the code being evaluated.
'use strict';
let x = 1;
eval('let x = 3; console.log(x);'); // LOGS: 3
console.log(x); // LOGS: 1
arguments
Strict mode simplifies arguments, by removing some of their
side effects. arguments aren’t aliased, thus they always
refer to the original arguments when the function was invoked. Moreover,
arguments.callee and arguments.caller are no
longer supported.
'use strict';
function f(x) {
x = 5;
return x === arguments[0];
}
f(10); // false
this boxing
Strict mode makes JavaScript more secure, by restricting access the global
object via this. In strict mode, this is not
boxed (forced into being an object), meaning that if unspecified it will
be undefined instead of the global object.
'use strict';
function f() {
return this;
}
f(); // undefined
Strict mode implements a few more, less commonly-mentioned changes. These include:
with is prohibited, resulting in an error if usedFunction.prototype.arguments and
Function.prototype.caller are non-deletable properties
which throw when set or retrieved
implements, interface, let, package, private, protected, public, static,
and yield are reserved keywords