Strict mode: The advantage of using strict mode is that errors in your code can be detected early and ECMAScript behavior that can cause programming errors can be caught.

Selective use of strict modes

Pragma: “Use strict”; Engines that support the strict pattern will start the pattern, while those that do not will ignore the compilation instructions when encountering an unassigned string literal.

  • Given this compilation indication in the global scope (outside the function), the entire script uses strict mode.
  • Given this compilation indication in function scope, the function will use strict mode
function test () {
    "use strict"; . }Copy the code

Strict schema specification


  1. Accidentally creating a global variable is not allowed. Assigning a value to an undeclared variable will raise a ReferenceError at execution time
// Undeclared variables
// Non-strict mode: create global variables
// Strict mode: Throws ReferenceError
message = "Hello world! ";
Copy the code
  1. The delete operator cannot be called on variables
// Delete variables
// Non-strict mode: silent fails
// Strict mode: Throws ReferenceError
var color = "red";
delete color;
Copy the code
  1. Strict mode also limits variable names. You cannot use identifiers such as implements, interface, let, package, private, protected, public, static, or yield. Using the above identifiers as variable names results in syntax errors.


  1. Assigning a read-only attribute raises TypeError
  2. Using the delete operator on properties of a non-configurable video system throws a TypeError
  3. Adding properties to nonextensible objects raises TypeErrors
  4. When using object literals, attribute names must be unique
// Same name attribute
// Non-strict mode: no errors, the second property prevails
// Strict mode: throws a syntax error
var person = {
    name: "Nicholas".name: "Greg"
Copy the code


  1. Strict mode requires that the parameters of a named function be unique
// Repeat the name parameter
// Non-strict mode: no errors, only the second parameter can be accessed
// Strict mode: throws a syntax error
function sum (num, num){
    // todo
Copy the code
  1. In non-strict mode, changing the value of a named parameter is also reflected in the Arguments object, whereas in strict mode the two values are completely independent
// Change the value of the named parameter
// Non-strict mode: changes are reflected in arguments
// Strict mode: changes are not reflected in arguments
function showValue(value) {
    value = "Foo";
    console.log(value); //"Foo" 
    console.log(arguments[0]); // Non-strict mode :"Foo", strict mode :"Hi"
Copy the code
  1. Arguments. callee and arguments.caller, in non-strict mode, these two attributes refer one to the function itself and the other to the calling function. In strict mode, TypeError is raised for each property accessed
/ / access the arguments. The callee
// Non-strict mode: no problem
// Strict mode: Raises TypeError
function factorial(num){
    if (num <= 1) {
        return 1;
    } else {
        return num * arguments.callee(num- 1)}}var result=factorial(5);
Copy the code
  1. Strict mode also restricts function names, disallowing implements, interface, let, package, private, protected, public, static, and yield
  2. Functions can only be declared at the top of a script and inside a function
// Declare functions in an if statement
// Non-strict mode: the function is promoted outside the if statement
// Strict mode: throws a syntax error
if (true) {function doSomething(){
        // todo }}Copy the code


  1. In strict mode, it no longer creates variables or functions in the containment context
// Use eval() to create variables
// Non-strict mode: the dialog box displays 10
// Strict mode: ReferenceError is raised when alert(x) is called
function doSomething(){
    eval("var x=10");
Copy the code
  1. Variables and functions can be declared in eval(), but these variables or functions are valid only in the special scope in which they are evaluated, and are then destroyed
"use strict";
var result = eval("var x=10, y=11; x+y");
alert(result);  / / 21
Copy the code

The eval and the arguments

Strict mode already explicitly disallows the use of eval and arguments as identifiers, as well as reading and writing their values.

// Refer to eval and arguments as variables
// Non-strict mode: No problem, no error
// Strict mode: throws a syntax error
var eval = 10;
var arguments = "Hello world!";
Copy the code

Inhibition of this

Null or undefined values are converted to global objects when the apply() or call() methods of functions are used in non-strict mode. In strict mode, the function’s this value is always the specified value, no matter what the specified value is.

// Access properties
// Non-strict mode: null is passed, and the function's this value is a global object
// Strict mode: Throw an error because this is null
var color = "red";
function displayColor(){
Copy the code

Other changes

  1. The with statement in non-strict mode can change the path to parse identifiers. In strict mode, with is simplified
// Non-strict mode: allowed
// Strict mode: throws a syntax error
Copy the code
  1. Strict mode removes octal literals from JavaScript
// Use octal literals
// Non-strict mode: the value is 8
// Strict mode: throws a syntax error
var value = 010;
Copy the code
  1. In strict mode, octal literals are treated as decimal literals starting with 0
// Parse octal literals with parseInt()
// Non-strict mode: the value is 8
Strict mode: the value is 10
var value = parseInt("010");
Copy the code