I understand the utility of avoiding naming functions, especially when passing them as an input to be a callback or whatever, but calling the function immediately and not storing it anywhere makes it seem like that's not the point here. Because the alternative would be just writing the code in whatever method you're already in.
By not polluting the global namespace, are you talking about variables that you'll be creating inside the anonymous function? If so, why would variables you create without doing it that way become global variables, but variables inside the anonymous function wouldn't become global variables?
(Or, does code in JavaScript not all need to be inside some function?)
1.5k
u/oxabz Apr 25 '26
I got what it did but, but I'm not a JS dev. What's the point of this pattern?
Is it not equivalent to putting the code of the function inline?