Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

What are the benefits of using an immediately invoked function? And (function(){})() vs ((function(){})())

What are the benefits of using an immediately invoked function?

And (function(){})() vs ((function(){})())

2 Answers

Immediately Invoked Functions or IFFE are great for some reasons

A) It's not called from anywhere else, which is why it is anonymous , but runs just after being created, it keeps your code more safe and secure.

B) To avoid polluting the global namespace, because all the variables used inside the IIFE are not visible outside its scope.

that's a brief and simple explanation. You can read more on the subject here

http://benalman.com/news/2010/11/immediately-invoked-function-expression/

and

http://javascriptissexy.com/12-simple-yet-powerful-javascript-tips/#Powerful_Uses_of_Immediately_Invoked_Function_Expressions

both

(function() {

})();

and

(function() {

}());

does the exact same thing. Just pick one and be consistent when you use it.