IIFE là hàm được định nghĩa và gọi ngay lập tức: (function() { const secret = 42; })() — biến secret không lọt ra global scope. Cú pháp arrow function: (() => { ... })().
Trước ES6, IIFE là cách duy nhất để tạo scope riêng vì chỉ có function scope. Dùng để: tránh ô nhiễm global scope, thực thi code khởi tạo một lần, tạo module pattern với private state. Ngày nay ít dùng hơn do có ES modules và block scope với let/const, nhưng vẫn hay gặp trong code cũ.
An IIFE is a function that is defined and called immediately: (function() { const secret = 42; })() — the variable secret does not leak into the global scope. Arrow function syntax: (() => { ... })().
Before ES6, IIFEs were the only way to create a private scope since only function scope existed. Uses: avoid polluting global scope, run initialization code once, create a module pattern with private state. Less common today due to ES modules and block scope with let/const, but still encountered in legacy code.