What's the primary difference between an IIFE and a regular function that you call immediately after defining it?
// Example 1:
function regularFunc() {
var x = 10;
return x * 2;
}
var result1 = regularFunc();
// Example 2:
var result2 = (function() {
var x = 10;
return x * 2;
})();
The primary difference is that the IIFE doesn't have a name and cannot be called again. In Example 1, 'regularFunc' is defined and then called, but the function remains in scope and can be called again later. In Example 2, the IIFE is defined and called once, but since it doesn't have a name (it's an anonymous function expression), there's no way to reference it again after execution. This is beneficial when you only need a function to run once (like initialization code) and don't want to leave a function reference taking up space in memory or potentially being called again accidentally. The IIFE achieves its purpose and then effectively disappears, leaving behind only its returned value or effects.