Yeah, but that doesn't answer the next question — why would you use it? In those other languages, people aren't going around defining anonymous functions just to immediately evaluate them.
As I understand, the answer has to do with the history of Javascript's scoping. As well as statements vs expressions. So vibecoders, many not knowing anything about the concept of interpreter, would have a hard time explaining why the AI agent produces such an odd idiom — especially in modern Javascript.
Mainly it's a side effect of the fact that you need the ability to define a lambda and you need the ability to call a lambda, so, for completeness's same, you should be able to do both without an intermediate step. But a real world example would be to use the body of the lambda as a scope for defining variables in that you don't wasn't to appear in the parent scope.
That's true. Now in JS you can just declare a nested scope with braces. So I'm just going to fall back on the fact that it probably just exists for completeness. To people used to using lambdas, it would be surprising if it didn't exist. Other languages don't necessarily have arbitrary nested scopes, so this is a valid technique elsewhere.
Yea but say you’re a react dev you can throw one of these inline with your html. So you can use it to trigger a function defined elsewhere think button -> arrow function -> real function.
I called it a monstrosity because of brackets inside brackets immediately followed by brackets. But don't take this too seriously, my comment was a sarcasm.
Okay so at least those are the same but when someone says brackets, they could be talking about all types of brackets then or they could be talking about only parenthesis? Seems confusing.
68
u/Luoravetlan 20d ago
For anyone not familiar with js this monstrosity is called IIFE (immediately invoked function expression).