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
Laurence kite
11,768 PointsModules in JavaScript
I am confused as to why a function definition is placed within a round brackets.?
(function () { // code })();
why is this different from
function() { //code } ();
1 Answer
Steven Parker
243,656 PointsThe parentheses ("round brackets") make the function declaration into an expression.
The expression represents the newly-declared function and allow it to be invoked using the other parentheses at the end.
You can skip the enclosing parentheses if there is something else (like a math operator or an assignment) that would make the declaration into an expression.
Also see my answer to your other question.