JavaScript Strict Mode (use strict)
JavaScript strict mode runs under strict conditions.
Using the "use strict" Directive
The "use strict" directive was introduced in JavaScript 1.8.5 (ECMAScript 5).
It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.
The purpose of "use strict" is to indicate that the code should be executed in strict mode.
You cannot use undeclared variables in strict mode.
| | Browsers that support strict mode: <br>Internet Explorer 10+, Firefox 4+, Chrome 13+, Safari 5.1+, Opera 12+. | | --- | --- |
Strict Mode Declaration
Strict mode is declared by adding use strict
; at the top of a script or a function.
In the example, you can press F12 (or click "Tools > More Tools > Developer Tools") in the browser to enable debug mode and view error messages.
You can also right-click and select "Inspect" to view the error messages, as shown in the GIF below:
Example
"use strict";
x = 3.14; // Error (x is not defined)
Example
"use strict";
myFunction();
function myFunction() {
y = 3.14; // Error (y is not defined)
}
Declaring strict mode within a function creates a local scope (strict mode only within the function):
Example
x = 3.14; // No error
myFunction();
function myFunction() {
"use strict";
y = 3.14; // Error (y is not defined)
}
Why use strict mode:
- Eliminates some unsafe aspects of code execution to ensure code safety;
- Improves compiler efficiency and increases runtime speed;
- Prepares for future versions of JavaScript.
"Strict mode" reflects JavaScript's direction towards being more reasonable, secure, and rigorous. Mainstream browsers, including IE 10, already support it, and many large projects have embraced it.
On the other hand, the same code may have different results in "strict mode"; some statements that run in "normal mode" may not run in "strict mode". Understanding these details helps you gain a deeper insight into JavaScript, making you a better programmer.
Restrictions in Strict Mode
Undeclared variables are not allowed:
"use strict";
x = 3.14; // Error (x is not defined)
| | Objects are also variables. | | --- | --- |
"use strict";
x = {p1:10, p2:20}; // Error (x is not defined)
Deleting variables or objects is not allowed.
"use strict";
var x = 3.14;
delete x; // Error
Deleting functions is not allowed.
"use strict";
function x(p1, p2) {};
delete x; // Error
Duplicate parameter names are not allowed:
"use strict";
function x(p1, p1) {}; // Error
Octal literals are not allowed:
"use strict";
var x = 010; // Error
Escape characters are not allowed:
"use strict";
var x = \010; // Error
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
Assigning to a property with a getter method is not allowed:
"use strict";
var obj = {get x() {return 0} };
obj.x = 3.14; // Error
Deleting a non-deletable property is not allowed:
"use strict";
delete Object.prototype; // Error
Variable names cannot use "eval" string:
"use strict";
var eval = 3.14; // Error
Variable names cannot use "arguments" string:
"use strict";
var arguments = 3.14; // Error
Statements like the following are not allowed:
"use strict";
with (Math){x = cos(2)}; // Error
Variables created by eval() within its scope cannot be called:
"use strict";
eval ("var x = 2");
alert (x); // Error
The this keyword cannot point to the global object.
function f(){
return !this;
}
// Returns false, because "this" points to the global object, "!this" is false
function f(){
"use strict";
return !this;
}
// Returns true, because in strict mode, this is undefined, so "!this" is true.
Therefore, if you forget to add new in a constructor, this will no longer point to the global object but will throw an error.
function f(){
"use strict";
this.a = 1;
};
f();// Error, this is not defined
Reserved Keywords
To transition to future versions of JavaScript, strict mode introduces some reserved keywords:
- implements
- interface
- let
- package
- private
- protected
- public
- static
- yield
"use strict"; var public = 1500; // Error
| | The "use strict" directive can only appear at the beginning of a script or function. | | --- | --- |