JavaScript strict mode

"use strict"; defines that JavaScript code should be executed in "strict mode".

"use strict" directive

"use strict" is a new directive in JavaScript 1.8.5 (ECMAScript version 5).

It is not a statement but an expression statement, and earlier versions of JavaScript would ignore it.

"use strict"; The purpose is to indicate that JavaScript code should be executed in "strict mode".

In strict mode, you cannot, for example, use undeclared variables.

The following versions of browsers support strict mode:

  • versions of IE after 10
  • versions of Firefox after 4
  • versions of Chrome after 13
  • versions of Safari after 5.1
  • versions of Opera after 12

declare strict mode

By adding at the beginning of a script or function "use strict"; to declare strict mode.

Declaring strict mode at the beginning of a script has global scope (all code in the script is executed in strict mode):

Example

"use strict";
x = 3.14;       // This will cause an error because x has not been declared

Try It Yourself

Example

"use strict";
myFunction();
function myFunction() {
     y = 3.14;   // This will cause an error because y has not been declared
}

Try It Yourself

Declaring strict mode within a function has local scope (only the code within the function is executed in strict mode):

x = 3.14;       // This will not cause an error
myFunction();
function myFunction() {
	"use strict";
	 y = 3.14;   // This will cause an error
}

Try It Yourself

"use strict" syntax

The syntax for declaring strict mode is designed to be compatible with earlier versions of JavaScript.

Compiling numeric literals (such as 4+5) or string literals ("Bill Gates") in JavaScript programs has no negative effect. It will simply compile to non-existent variables and then disappear.

所有 "use strict"; All

It will only affect new compilers that 'understand' its meaning.

Why use strict mode?

Strict mode makes it easier to write 'safe' JavaScript.

Strict mode turns previously acceptable 'bad syntax' into real errors.

In ordinary JavaScript, if a variable name is misspelled, it will create a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

In strict mode, assigning a value to an unwritable, read-only, non-existent property, or assigning a value to a non-existent variable or object, will throw an error.

Things not allowed in strict mode

Using a variable without declaring it is not allowed:

"use strict";
x = 3.14;                // This will cause an error

Try It Yourself

An object is also a variable

Using an object without declaring it is not allowed:

"use strict";
x = {p1:10, p2:20};      // This will cause an error

Try It Yourself

Deleting a variable (or an object) is not allowed:

"use strict";
var x = 3.14;
delete x;                // This will cause an error

Try It Yourself

Deleting a function is not allowed:

"use strict";
function x(p1, p2) {}; 
delete x;                 // This will cause an error

Try It Yourself

Duplicate parameter names are not allowed:

"use strict";
function x(p1, p1) {};   // This will cause an error

Try It Yourself

Octal numeric literals are not allowed:

"use strict";
var x = 010;             // This will cause an error

Try It Yourself

Escape characters are not allowed:

"use strict";
var x = \010;            // This will cause an error

Try It Yourself

Writing to a read-only property is not allowed:

"use strict";
var obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14;            // This will cause an error

Try It Yourself

Writing to a property that can only be read is not allowed:

"use strict";
var obj = {get x() {return 0} };
obj.x = 3.14;            // This will cause an error

Try It Yourself

Deleting non-deletable properties is not allowed:

"use strict";
delete Object.prototype; // This will cause an error

Try It Yourself

The string "eval" cannot be used as a variable:

"use strict";
var eval = 3.14;         // This will cause an error

Try It Yourself

The string "arguments" cannot be used as a variable:

"use strict";
var arguments = 3.14;    // This will cause an error

Try It Yourself

with The statement is not allowed:

"use strict";
with (Math){x = cos(2)}; // This will cause an error

Try It Yourself

For security reasons, it is not allowed eval() to create variables in the scope where it is called:

"use strict";
eval ("var x = 2");
alert (x);               // This will cause an error

Try It Yourself

In function calls similar to f(), the value of 'this' is the global object. In strict mode, it has become undefined.

Future Proofing

Keywords reserved for future use are not allowed in strict mode. They are:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield
"use strict";
var public = 1500;      // This will cause an error

Try It Yourself

Warning

"use strict" Instructions can only be used in scripts or functions'StartRecognized.