Strict Mode (JavaScript)

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 Restrictions on Code in Strict Mode section later in this topic.

You can declare strict mode by adding "use strict"; at the beginning of a file, a program, or a function. This kind of declaration is known as a directive prologue. The scope of a strict mode declaration depends on its context. If it is declared in a global context (outside the scope of a function), all the code in the program is in strict mode. If it is declared in a function, all the code in the function is in strict mode. For example, in the following example all the code is in strict mode, and the variable declaration outside the function causes the syntax error "Variable undefined in strict mode."

"use strict";
function testFunction(){
    var testvar = 4;
    return testvar;
testvar = 5;

In the following example, only the code inside testFunction is in strict mode. The variable declaration outside the function does not cause a syntax error, but the declaration inside the function does.

function testFunction(){
    "use strict";
    testvar = 4;
    return testvar;
testvar = 5;

The following table lists the most important restrictions that apply in strict mode.

Language element




Using a variable without declaring it.

testvar = 4;

Read-only property

Writing to a read-only property.

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 whose extensible attribute is set to false.

Object.defineProperty(testObj, "testvar", {
    value: 10,
    extensible: false
delete testObj.testvar;


Deleting a variable, a function, or an argument.

Deleting a property whose configurable attribute is set to false.

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.

var testObj = {
    prop1: 10,
    prop2: 15,
    prop1: 20

Duplicating a parameter name

Using a parameter name more than once in a function.

function testFunc(param1, param1) {
    return 1;

Future reserved keywords

Using a future reserved keyword as a variable or function name.

  • implements

  • interface

  • let

  • package

  • private

  • protected

  • public

  • static

  • yield


Assigning an octal value to a numeric literal, or attempting to use an escape on an octal value.

var testoctal = 010;
var testescape = \010;


The value of this is not converted to the global object when it is null or undefined.

function testFunc() {
    return this;
var testvar = testFunc();

In non-strict mode, the value of testvar is the global object, but in strict mode the value is undefined.

eval as an identifier

The string "eval" cannot be used as an identifier (variable or function name, parameter name, and so on).

var eval = 10;

Variable declared inside an eval function

If a variable is declared inside an eval function, it cannot be used outside that function.

eval("var testvar = 10");
testvar = 15;

Arguments as an identifier

The string "arguments" cannot be used as an identifier (variable or function name, parameter name, and so on).

var arguments = 10;

arguments inside a function

You cannot change the values of members of the local arguments object.

function testArgs(oneArg) {
    arguments[0] = 20;

In non-strict mode, you can change the value of the oneArg parameter by changing the value of arguments[0], so that the value of both oneArg and arguments[0] is 20. In strict mode, changing the value of arguments[0] does not affect the value of oneArg, because the arguments object is merely a local copy.


Not allowed.

function (testInt) {
    if (testInt-- == 0)


Not allowed.

with (Math){
    x = cos(3);
    y = tan(7);

The indirect use of eval is allowed even in strict mode. A common way of using indirect eval is to set a variable to it and call the variable.

var indirectEval = eval;
indirectEval("var testvar = 10;");

Community Additions

© 2014 Microsoft. All rights reserved.