Before we begin, please understand that I dislike the argument “that’s just semantics”. For the record, I’m also not fond of “it’s all relative”, but I’ll save that rant for another post. If semantics aren’t important then neither are spelling, grammar, punctuation, and pronunciation. I have had a long uphill battle trying to convince my children that words and phrases have meaning, and the terms “whatever” and “you know what I mean” are not Get Out of Jail Free cards for proper use of the English language. They are surely sick of hearing me say “words matter”.
This practice is commonly referred to as a “self-executing function” or sometimes “self-invoking”. The function, in this case anonymous, is wrapped in a set of parentheses which contains the scope of its variables within the function, hence protecting them from outside interference. The second set of parentheses trigger the execution of the code contained within the first set. In other words, the first set of parentheses defines the function and the second set immediately executes the function. This syntax is also used to differentiate between function declarations and function expressions.
There’s plenty more we could discuss about this, and even a few alternate syntax examples, but I don’t want to get too far away from my point, which is this is not “self-executing”. The parentheses that execute the function are defined outside the function boundaries. A truly self-executing function would cause itself to execute, which I don’t believe is possible. Recursion is close, but it still couldn’t account for the initial function execution. More accurately, this is an “immediately executed” function.
This post was going to be more long winded but I found a great article aptly titled Immediately-Invoked Function Expression by Ben Alman that does a much better job of explaining the details than I could have. The acronym (IIFE) may be pronounced “iffy”, but there’s no question in my mind it’s the better choice.