1

We can have immediately invoking function in two ways. I am confused about what is the difference between the following

var foo = function(){
   return { };
}();

and this :

var foo = (function(){
   return { };
}());
Yeasin Abedin
  • 1,639
  • 2
  • 19
  • 33

1 Answers1

5

Exactly the same.

// This one creates a function expression, then executes that function expression.
var foo = function(){
   return { };
}();

// This one creates a function expression, inside of a set of parens.
// the parens hold an expression.
var foo = (function(){
   return { };
}());

The parens are used for two reasons:

1) In this context, they are a clue to the READER, not to the compiler, that you have an IIFE.

2) In other contexts, the parens force a expression, when a function statement might be generated.

// The parens here force an expression, which means it forces a function expression
// instead of a function statement.
(function () {....})
Jeremy J Starcher
  • 22,523
  • 6
  • 52
  • 73