Strict mode is a way to introduce better error-checking into your code. When you use strict mode, you cannot, for example, use implicitly declared variables, or assign a value to a read-only property, or add a property to an object that is not extensible. The restrictions are listed in the
Strict mode is not supported in versions of Internet Explorer earlier than Internet Explorer 10. |
You can declare strict mode by adding
"use strict"; function testFunction(){ var testvar = 4; return testvar; } // This causes a syntax error. testvar = 5;
In the following example, only the code inside
function testFunction(){ "use strict"; // This causes a syntax error. testvar = 4; return testvar; } testvar = 5;
The following table lists the most important restrictions that apply in strict mode.
Language element | Restriction | Error | Example |
Variable | Using a variable without declaring it. | SCRIPT5042: Variable undefined in strict mode |
JavaScript
testvar = 4; |
Read-only property | Writing to a read-only property. | SCRIPT5045: Assignment to read-only properties is not allowed in strict mode |
JavaScript
var testObj = Object.defineProperties({}, { prop1: { value: 10, writable: false // by default }, prop2: { get: function () { } } }); testObj.prop1 = 20; testObj.prop2 = 30; |
Non-extensible property | Adding a property to an object whoseextensibleattribute is set tofalse. | SCRIPT5046: Cannot create property for a non-extensible object |
JavaScript
var testObj = new Object(); Object.preventExtensions(testObj); testObj.name = "Bob"; |
delete | Deleting a variable, a function, or an argument. Deleting a property whoseconfigurableattribute is set tofalse. | SCRIPT1045: Calling delete on is not allowed in strict mode |
JavaScript
var testvar = 15; function testFunc() {}; delete testvar; delete testFunc; Object.defineProperty(testObj, "testvar", { value: 10, configurable: false }); delete testObj.testvar; |
Duplicating a property | Defining a property more than once in an object literal. | SCRIPT1046: Multiple definitions of a property not allowed in strict mode |
JavaScript
var testObj = {
prop1: 10,
prop2: 15,
prop1: 20
};
|
Duplicating a parameter name | Using a parameter name more than once in a function. | SCRIPT1038: Duplicate formal parameter names not allowed in strict mode |
JavaScript
function testFunc(param1, param1) { return 1; }; |
Future reserved keywords | Using a future reserved keyword as a variable or function name. | SCRIPT1050: The use of a future reserved word for an identifier is invalid. The identifier name is reserved in strict mode. |
|
Octals | Assigning an octal value to a numeric literal, or attempting to use an escape on an octal value. | SCRIPT1039: Octal numeric literals and escape characters not allowed in strict mode |
JavaScript
var testoctal = 010; var testescape = \010; |
this | The value of | |
JavaScript
function testFunc() { return this; } var testvar = testFunc(); In non-strict mode, the value of |
eval | The string "eval" cannot be used as an identifier (variable or function name, parameter name, and so on). | |
JavaScript
var eval = 10;
|
Function declared inside a statement or a block | You cannot declare a function inside a statement or a block. | SCRIPT1047: In strict mode, function declarations cannot be nested inside a statement or block. They may only appear at the top level or directly inside a function body. |
JScript
var arr = [1, 2, 3, 4, 5]; var index = null; for (index in arr) { function myFunc() {}; } |
Variable declared inside an | If a variable is declared inside an | SCRIPT1041: Invalid usage of 'eval' in strict mode |
JavaScript
eval_r("var testvar = 10");
testvar = 15;
Indirect evaluation is possible, but you still cannot use a variable declared outside the
JavaScript
var indirectEval = eval; indirecteval_r("var testvar = 10;"); document.write(testVar); This code causes an error SCRIPT5009: 'testVar' is undefined. |
Arguments | The string "arguments" cannot be used as an identifier (variable or function name, parameter name, and so on). | SCRIPT1042: Invalid usage of 'arguments' in strict mode |
JavaScript
var arguments = 10;
|
arguments | You cannot change the values of members of the local | |
JavaScript
function testArgs(oneArg) {
arguments[0] = 20;
}
In non-strict mode, you can change the value of the |
arguments.callee | Not allowed. | |
JavaScript
function (testInt) { if (testInt-- == 0) return; arguments.callee(testInt--); } |
with | Not allowed. | SCRIPT1037: 'with' statements are not allowed in strict mode |
JavaScript
with (Math){ x = cos(3); y = tan(7); } |