2

I can't get my head around this syntax, what does the set of curly-braces after "use strict" do?

function test(a, b) {
   'use strict';
   {
      var c = {};
      Array.prototype.slice
   }

   ... //more stuff
   return a;
}
Amine
  • 348
  • 2
  • 9
  • They introduce a block statement. Since blocks in JavaScript have no scope, there's no much point in doing this. – The Paramagnetic Croissant Oct 28 '14 at 15:04
  • Note that the upcoming ECMAScript 6 spec allows block-scoped variables, with `let` statements. Since that's currently only supported in Firefox, there's no real reason to do this outside of Firefox-specific code. See also [Do standalone JavaScript blocks have any use?](http://stackoverflow.com/q/17939230/710446) – apsillers Oct 28 '14 at 15:23

1 Answers1

1

It is just a regular block. In Javascript, blocks like those have no specific scope. They follow the flow of the code as it is.

This means that you can safely ignore (or remove) them without any effect on the code.

function test(a, b) {
   'use strict';
    // no curly braces here
      var c = {};
      Array.prototype.slice
   // neither here

   ... //more stuff
   return a;
} //one and the same thing

You can read more about scope in Javascript in this Stackoverflow post.

Community
  • 1
  • 1
user3459110
  • 6,961
  • 3
  • 26
  • 34