By dopeddude


2015-05-08 06:55:26 8 Comments

//Global Scope
"use strict"; //1
function A() {
    "use strict"; //2
    function innerA() {
        "use strict"; //3
    }
}

I was just wondering:

Is doing use strict at //1 is enough or do we have to be explicit at all places like //2 and //3.

2 comments

@thefourtheye 2015-05-08 07:01:17

Quoting MDN on strict mode,

To invoke strict mode for an entire script, put the exact statement "use strict"; (or 'use strict';) before any other statements.

concatenating strict and non-strict scripts is problematic. It is thus recommended that you enable strict mode on a function-by-function basis.

So putting it at the top applies to the entire file. You don't have to explicitly mention that in every function.

Note: Using use strict at the top has its own problems. Read about them in the linked MDN page. So, the recommended approach, as per MDN is

You can also take the approach of wrapping the entire contents of a script in a function and having that outer function use strict mode. This eliminates the concatenation problem but it means that you have to explicitly export any global variables out of the function scope.


You can test that, like this

'use strict';

(function () {
    return {
        1: 1,
        1: 2
    };
})();

Now, it will throw an error,

SyntaxError: Duplicate data property in object literal not allowed in strict mode

But, when you do something like this

(function () {
    return {
        1: 1,
        1: 2
    };
})();

(function () {
    'use strict';
    return {
        1: 1,
        1: 2
    };
})();

it will fail only in the second function, not in the first function. Because, only the second function is in strict mode.

Also, if you had a function within a function, like you have shown in the question,

(function () {
    'use strict';
    (function () {
        return {
            1: 1,
            1: 2
        };
    })();
})();

the inner function will also be in the strict mode because of the use strict in the enclosing function. So, the inner function will raise a SyntaxError.


But, if you use use strict in a block within {}, it will not have any effect, for example,

(function () {
    {
        'use strict';
        return {
            1: 1,
            1: 2
        };
    }
})();

or

console.log("");

'use strict';

var a = {
    1: 1,
    1: 2
};

will not throw any error.


So, use strict should be at the beginning of a function, or at the beginning of a file. Only then the code will be in strict mode.

@Rob Johansen 2015-05-08 07:03:08

Defining it at //1 is enough. This is straight from JavaScript: The Definitive Guide (with emphasis added by me):

The top-level (nonfunction) code of a script is strict code if the script has a "use strict" directive. A function body is strict code if it is defined within strict code or if it has a "use strict" directive.

Related Questions

Sponsored Content

63 Answered Questions

[SOLVED] How can I merge properties of two JavaScript objects dynamically?

28 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

27 Answered Questions

86 Answered Questions

[SOLVED] How do JavaScript closures work?

49 Answered Questions

[SOLVED] How to check if an object is an array?

26 Answered Questions

[SOLVED] What is the scope of variables in JavaScript?

41 Answered Questions

[SOLVED] How do I loop through or enumerate a JavaScript object?

7 Answered Questions

[SOLVED] Why does Google prepend while(1); to their JSON responses?

36 Answered Questions

[SOLVED] $(document).ready equivalent without jQuery

  • 2009-04-28 21:51:11
  • FlySwat
  • 1054724 View
  • 2048 Score
  • 36 Answer
  • Tags:   javascript jquery

17 Answered Questions

[SOLVED] How to decide when to use Node.js?

Sponsored Content