JavaScript Strict Mode (use strict)

JavaScript strict mode runs under strict conditions.


Using the "use strict" Directive

The "use strict" directive is new in JavaScript 1.8.5 (ECMAScript5).

It is not a statement, but a literal expression, and is ignored in older versions of JavaScript.

The purpose of "use strict" is to specify that the code should be executed under strict conditions.

In strict mode, you cannot use undeclared variables.


Note Browsers supporting strict mode:
Internet Explorer 10 +、Firefox 4+、 Chrome 13+、 Safari 5.1+、 Opera 12+。

Strict Mode Declaration

Strict mode is declared by adding at the beginning of a script or functionuse strict; expression to declare.

In the example, we can press in the browserF12 (or click "Tools > More Tools > Developer Tools")Enable debug mode and view error messages.

You can also right-click and select"Inspect"to view. The GIF demonstration is as follows:

Example

"use strict";
x = 3.14;       // Error (x is not defined)

Try it »

Example

"use strict";
myFunction();

function myFunction() {
    y = 3.14;   // Error (y is not defined)
}

Try it »

Declaring strict mode inside a function is local scope (strict mode is applied only inside the function):

Example

x = 3.14;       // No error
myFunction();

function myFunction() {
   "use strict";
    y = 3.14;   // Error (y is not defined)
}

Try it »

Why use strict mode:

  • Eliminate some unreasonable and imprecise aspects of JavaScript syntax, and reduce some strange behaviors.
  • Eliminate some unsafe aspects of code running and ensure the safety of code execution.
  • Improve compiler efficiency and increase running speed.
  • Pave the way for future new versions of JavaScript.

"Strict mode" reflects a more reasonable, safer, and more rigorous development direction for JavaScript. Mainstream browsers, including IE 10, already support it, and many large projects have begun to fully embrace it.

On the other hand, the same code may produce different results in "strict mode"; some statements that work in "normal mode" will not work in "strict mode". Mastering these contents will help you understand JavaScript more deeply and make you a better programmer.


Strict Mode Restrictions

Undeclared variables are not allowed:

"use strict";
x = 3.14;                // Error (x is not defined)

Try it »
Note An object is also a variable.

"use strict";
x = {p1:10, p2:20};      // Error (x is not defined)

Try it »

Deleting a variable or object is not allowed.

"use strict";
var x = 3.14;
delete x;                // Error

Try it »

Deleting a function is not allowed.

"use strict";
function x(p1, p2) {};
delete x;                // Error

Try it »

Duplicate variable names are not allowed:

"use strict";
function x(p1, p1) {};   // Error

Try it »

Octal literals are not allowed:

"use strict";
var x = 010;             // Error

Try it »

Escape characters are not allowed:

"use strict";
var x = \010;            // Error

Try it »

Assignment to a read-only property is not allowed:

"use strict";
var obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});

obj.x = 3.14;            // Error

Try it »

Assigning to a property that is read using a getter method is not allowed:

"use strict";
var obj = {get x() {return 0} };

obj.x = 3.14;            // Error

Try it »

Deleting an undeletable property is not allowed:

"use strict";
delete Object.prototype; // Error

Try it »

Variable names cannot use the string "eval":

"use strict";
var eval = 3.14;         // Error

Try it »

Variable names cannot use the string "arguments":

"use strict";
var arguments = 3.14;    // Error

Try it »

The following statement is not allowed:

"use strict";
with (Math){x = cos(2)}; // Error

Try it »

For security reasons, variables created within the scope of eval() cannot be called:

"use strict";
eval ("var x = 2");
alert (x);               // Error

Try it »

The this keyword is forbidden to point to the global object.

function f(){
    return !this;
} 
// 返回false,因为"this"指向全局对象,"!this"就是false

function f(){ 
    "use strict";
    return !this;
} 
// 返回true,因为严格模式下,this的值为undefined,所以"!this"为true。

Therefore, when using a constructor, if you forget to add new, this no longer points to the global object, but instead throws an error.

function f(){
    "use strict";
    this.a = 1;
};
f();// 报错,this未定义

Reserved Keywords

In order to transition to future new versions of JavaScript, strict mode adds some reserved keywords:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield
"use strict";
var public = 1500;      // Error

Try it »

Note The "use strict" directive is only allowed to appear at the beginning of a script or function.
Other Extensions