Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Strict Mode (use strict)

JavaScript Strict Mode (use strict)

JavaScript strict mode means running under strict conditions.


Using the 'use strict' directive

The 'use strict' directive was added in JavaScript 1.8.5 (ECMAScript 5).

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

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

In strict mode, you cannot use undeclared variables.

| Note | Browsers that support strict mode:
Internet Explorer 10 +、Firefox 4+、 Chrome 13+、 Safari 5.1+、 Opera 12+。 | | --- | --- |


Declaring strict mode

Strict mode is declared by adding the use strict; expression at the beginning of a script or function.

In the example, we can press in the browser F12 (or click 'Tools > More tools > Developer tools') to open debug mode and view error messages.

You can also right-click and select "Inspect" to view it; the GIF demo is shown below:

Example

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

Try it »

Example

"use strict";
myFunction();

 function myFunction() {      

      y = 3.14;     // 报错 (y 未定义)       

}

Try it »

Declared inside a function, it has local scope (strict mode applies only inside the function):

Example

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

function   myFunction() {      

    "use strict";      

      y = 3.14;     // 报错 (y 未定义)       

}

Try it »


Why use strict mode:

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

On the other hand, the same code may behave differently in 'strict mode'; some statements that work in 'normal mode' will not run in 'strict mode'. Understanding these details helps you understand JavaScript more deeply and become a better programmer.


Restrictions of strict mode

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 variables or objects is not allowed.

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

Try it »

Deleting functions 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 sequences are not allowed:

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

Try it »

Assigning to read-only properties 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 read through a getter method is not allowed

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

obj.x = 3.14; // Error

Try it »

Deleting a property that cannot be deleted 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 kind of statement is not allowed:

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

Try it »

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

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

Try it »

The this keyword is forbidden from referring to the global object.



function f(){

    return !this;

} 

// returns false, because "this" points to the global object, and "!this" is false



function f(){ 

    "use strict";

    return !this;

} 

// Returns true because in strict mode, this is undefined, so "!this" is true.



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



function f(){

    "use strict";

    this.a = 1;

};

f();// 报错,this未定义


Reserved Keywords

To ease the transition to future versions of JavaScript, strict mode adds some reserved keywords:

"use strict";
var public = 1500; // Error

Try it »

Note The 'use strict' directive is only allowed at the beginning of a script or function.