Possible Duplicate:
What do empty parentheses () after a function declaration do in javascript?
I am looking at some Javascript code and trying to figure out what }(); right after return num+10 means. Does that mean the function will get executed immediately? Where can I get more information about this.
function addToTen(num) {
return function() {
return num+10;
}();
}
addToTen(5); // 15
Thanks,
Venn.
Yes, but only if it’s a function expression, which is different from a function declaration. A function declaration is how your first function is defined:
If you add
()after this, you get a Syntax Error. If the function is defined as a function expression, adding a set of parenthesis immediately executes it. Functions are expressions whenever they are not defined as above, e.g.:Your actual example is just… odd, and pointless. It’s almost a classic closure example, something like:
but your actual example is equivalent to just:
and all the extra stuff is completely unnecessary.