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

Is there a difference between '+' and '!' for JavaScript self-calling anonymous function modular pattern?

(from Track "The Module Pattern in JavaScript")

4 Answers

Hi Brac,

Can you give more context?

Regarding wrapping all code in a single JavaScript file with a self-executing anonymous function, i.e.

;(function(){
    // ...code...
}());

In multiple files, so ; is needed at beginning as well so that file inclusion is not order dependent. Can simplify to:

+function(){
    // ...code...
}();

OR

!function(){
    // ...code...
}();

Reference first 5 minutes of: https://teamtreehouse.com/library/the-module-pattern-in-javascript-2

My question is -- is there a functional difference between using the + and !, or is it just personal preference?

Thanks, Brac

Yeah, my answer would be no.

Best practice is to ;

Between that, module.exports and great server side bundlers you shouldn't have a problem.

Thanks for the reply Ryan. To clarify -- you state there is no functionality difference between the three code examples above AND you would suggest sticking with the first, more verbose code example?

Yes

Thanks Ryan!