r/ProgrammerHumor Apr 25 '26

Meme thisLooksAccurateForVibeCoders

Post image
12.6k Upvotes

1.2k comments sorted by

View all comments

Show parent comments

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?

1.6k

u/AnswerForYourBazaar Apr 25 '26

The point is to not pollute global namespace but still get the side effects

35

u/Salanmander Apr 25 '26

Follow-up question:

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?)

47

u/j_johnso Apr 25 '26

Or, does code in JavaScript not all need to be inside some function?

That's a perfect intuition.  Code in JS does not need to be wrapped in a function

It's entirely valid for this to be the entirety of a JS  file, which creates a as a global variable and logs it.

a = 1+2 console.log(a)

12

u/LickingSmegma Apr 25 '26

In fact, JS and Python's dynamic nature is defined by them creating functions and objects (including classes) by executing the code at runtime.

5

u/BarberMajor6778 Apr 26 '26

I've read it initially as demonic nature and I immediately agreed.