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
Craig Curtis
19,985 PointsWhat 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
Fernando Boza
25,384 PointsImmediately 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
Thomas Nilsen
14,957 Pointsboth
(function() {
})();
and
(function() {
}());
does the exact same thing. Just pick one and be consistent when you use it.