Search code examples
javascriptfunctioncurly-braces

Javascript, strict mode - These curly braces doesn't seem to be an object literal, what do they do?


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;
}

Solution

  • 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.