2

For example, I have code like this:

function test() {
  // some code
}

function main() {
  "use strict";
  test();
}

As you can see - in function main I have enabled strict mode.

Question: does this enable strict mode in function test or I need to define strict mode for each function separately?

Thanks.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Roman Nazarkin
  • 2,209
  • 5
  • 23
  • 44
  • You might want to check [this answer](http://stackoverflow.com/a/30117694/1903116) as well, it has few examples explaining it. – thefourtheye Jun 27 '15 at 05:20

2 Answers2

8

No, it doesn't. Strict mode is scoped. So only the code within your main is in strict mode.

Note that while strict mode is scoped, some effects of it can be seen outside of strict mode code. That isn't the case with your code, but I thought I should mention it. This question and its answers go into that in more detail.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
-1

No, since javascript has function level scope, right now strict is scoped to the function main only. If you want it to be scoped to other functions, you can either specify "use strict"; globally or specify it for required functions individually.

Note: specifying it gloablly will affect all the functions whatsoever.