0

I have seen both of these syntaxes (the second one a lot more though)

Is there a difference? Are they both behaving the same way?

(function() {
  // code
}())
(function() {
  // code
})()
K-Yo
  • 1,250
  • 10
  • 14
  • 5
    They're the same. I know this question is a duplicate – Ian Aug 07 '13 at 17:06
  • Neither are closures if there are no variables in the outer scope. Both are closures if there are. – Paul Aug 07 '13 at 17:08
  • 1
    No closure unless they export a function. What you showed us is just an [IEFE](http://benalman.com/news/2010/11/immediately-invoked-function-expression/). – Bergi Aug 07 '13 at 17:08
  • I did the following in the chrome console `var g = (function() { alert(1) }());` and `var h = (function() { alert(2) })();` and finally `h===g;` it returns true. I know it does not help but i found it courious – surfmuggle Aug 07 '13 at 17:10
  • 2
    @threeFourOneSixOneThree Look at what `g` and `h` actually contain. They are `undefined`, so of course they are `===`. The inner functions don't return anything (they just `alert`) – Ian Aug 07 '13 at 17:11

0 Answers0