The “strict mode” was introduced in ECMAScript 5. It allows you to put your script into the “strict” operating mode which prevents certain (potentially dangerous) operations.
ES6+ classes and native ECMAScript modules have strict mode enabled by default.
Strict mode features and limitations
JavaScript “use strict” statement imposes some restrictions on what you can do:
- No global variables
- Function parameter names must be unique (no duplicate parameters)
- Can’t introduce new variables with
eval
- Static scoping
- No octal literals
- An attempt to delete a non-configurable property throws an error
- Using keywords like
implements
,interface
,let
,package
,private
,protected
,public
,static
, oryield
as variable names will throw an error
How to put your JS file in a strict mode
To put your *.js
file into the strict mode, you should put the line “use strict”
in the very first line of the file.
"use strict"
You can also put only a single code block (like a function) in a strict mode.
// regular js code
function strictFunction() {
"use strict";
// strict mode starts here
// ...
// and ends at the closing curly brace
}
// regular js code