(0) in final of javascript function node.js


I see a part of a code with something like this


//....some code


Open in new window

what means the (0) after the function and with the function is enclosed in parenthesis?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Well, I don't know if this code is actually just an example because in this specific example it doesn't make any sense.

What you have here is a self invoking function.
The main objective is to prevent declarations in the global namespace on an initialization piece of code.
As this is an anonymous function, there's no way to call it after, so the sole objective is to wrap it in parentheses and call it right away by adding () right after.

Passing arguments into this function (like you're passing the 0) is mainly used to avoid naming collisions. For instance, you'll find this a lot in jquery plug-ins to avoid collisions with $.

Here's a nice article if you want to further read about self invoking functions : https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hans LangerCommented:
so, since you can assign a function into a variable like this:
var func1 = myFunction(arg1){ return arg1 } 

Open in new window

and then execute it:
var result = func1(1)  //or var result =(func1)(1)
//result value will be = 1

Open in new window

you can do the same without assigning it to a variable and just execute it after being created.

//same as (func1)()
(function(arg1){return arg1})(0) <-- Executed with the first argument = 0

Open in new window

Is not very usefull but some people prefer to use it like that for readability and  save memory space.
joyacv2Author Commented:
thank you to all!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.