What is JavaScript’s strict mode?
30 December 2022 (Updated 31 December 2022)
On this page
In a nutshell
Strict mode in JavaScript makes several changes:
- Throws errors for otherwise silent JavaScript errors.
- Fixes mistakes that otherwise make it difficult for JavaScript engines to optimize code. As a result, strict mode can sometimes be more faster than non-strict mode.
- Prohibits syntax that’s likely to be used in future ECMAScript versions.
Changes introduced in strict mode
No assignment to undeclared variables
In non-strict mode, the below code wouldn’t throw any errors.
"use strict";
let mistypeVariable;
// Assuming no global variable mistypeVarible exists
// this line throws a ReferenceError due to the
// misspelling of "mistypeVariable" (lack of an "a")
mistypeVarible = 17;
Throws errors if object property assignment fails
In non-strict mode, the below code wouldn’t throw any errors.
"use strict";
// Assignment to a non-writable global
var undefined = 5; // TypeError
var Infinity = 5; // TypeError
// Assignment to a non-writable property
const obj1 = {};
Object.defineProperty(obj1, "x", { value: 42, writable: false });
obj1.x = 9; // TypeError
// Assignment to a getter-only property
const obj2 = {
get x() {
return 17;
},
};
obj2.x = 5; // TypeError
// Assignment to a new property on a non-extensible object
const fixed = {};
Object.preventExtensions(fixed);
fixed.newProp = "ohai"; // TypeError
this
never refers to globalThis
In sloppy mode (non-strict mode), if this
is not specified, it will refer to the global context. In strict mode, it’ll be undefined
.
"use strict";
function fun() {
return this;
}
console.assert(fun() === undefined);
Throws error if deleting an object property fails
In non-strict mode, the below code wouldn’t throw any errors.
"use strict";
delete Object.prototype; // TypeError
delete [].length; // TypeError
eval
doesn’t introduce new variables into the surrounding scope
var x = 17;
var evalX = eval("'use strict'; var x = 42; x;");
console.assert(x === 17);
console.assert(evalX === 42);
No duplicate parameter names
function sum(a, a, c) {
// syntax error
"use strict";
return a + a + c; // wrong if this code ran
}
No setting properties on primitive values
"use strict";
false.true = ""; // TypeError
(14).sailing = "home"; // TypeError
"with".you = "far away"; // TypeError
More reserved keywords
The following words can’t be used as variable names:
implements
interface
let
package
private
protected
public
static
yield
How to invoke strict mode
Scripts
// Whole-script strict mode syntax
"use strict";
const v = "Hi! I'm a strict mode script!";
Functions
function myStrictFunction() {
// Function-level strict mode syntax
"use strict";
function nested() {
return "And so am I!";
}
return `Hi! I'm a strict mode function! ${nested()}`;
}
function myNotStrictFunction() {
return "I'm not strict.";
}
Other notes
- Strict mode is enabled by default for JavaScript modules or classes.
Tagged:
JavaScript
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment