Why `(() => {})();` Actually Works?
I was just scrolling Twitter when I found this weird snippet of JavaScript:

At first glance, it looked like total gibberish. But I got curious...
I dropped it into my Windsurf, added a debugger, and to my surprise, it actually stopped execution like a real program. That made me dig deeper: what on earth is happening here?
What’s an IIFE?
This snippet is an example of an IIFE, which stands for Immediately Invoked Function Expression.
In plain English: it’s a function that you define and then immediately run. No saving it for later, no calling it separately — it runs on the spot.
Example
(function () {
console.log("Hello from an IIFE!");
})();
What happens here:
function () { ... }defines a function.- The outer
(...)turns it into an expression (so JS treats it as a value, not a declaration). - The trailing
()calls it right away.
Output:
Hello from an IIFE!
So the tiny Twitter snippet (() => {})(); is just the smallest possible IIFE written with an arrow function.
Backtracing: IIFE ➜ Plain Function
Let’s start from the normal function and slowly peel it back until we reach the IIFE.
1. Normal Function
function add(a, b) {
console.log(a + b);
}
add(1, 2);
- I hope you don’t find this too much of a stretch.
- A normal function
add()which takes two arguments and logs their sum. - Output:
3
2. Let's directly call the function
(function (a, b) {
console.log(a + b);
})(1, 2);
- Outer
(...)→ make the function an expression. If you don't wrap it in(...)JS will throw a syntax error that identifier was expected.
- Trailing
(1, 2)→ immediately invoke it with arguments1and2. - Inside is the function written as previous step.
- Output:
3
3. Let's remove the arguments to make it simpler
(function () {
console.log(1+2);
})();
- Almost the same as previous step but without arguments.
- Output:
3
4. Let's make this an arrow function
(() => {
console.log(1+2);
})();
- Arrow function instead of
function. - Output:
3
Now you must be seeing somewhat similar to the Twitter snippet, right?
5. No console statement
(() => {})();
- This is the smallest possible IIFE written with an arrow function.
- Output: No output
Without the outer (...), writing this:
() => {}()
is a syntax error. Why? Because JS tries to treat the {} as the function body and then sees () hanging after it. That doesn’t parse.
we’re telling JS: “this whole arrow function is a single expression.” Now the () can cleanly call it.
When to Use IIFEs?
- To run some code immediately on load.
- To keep variables private (scoped inside the function).
- To do setup work once without polluting global scope.
Today, modules often replace the need for IIFEs, but they’re still a neat trick that shows how JavaScript expressions work under the hood.

