Both of these examples of closures have the benefit of preserving the value of count in subsequent function calls, whereas otherwise (if a closure were not used), count would reset to 0 following each function invocation. Further, in both cases the count variable (as any let variable in a block) is private, that is, not globally accessible.
The IFFE closure example was adapted from Marius Schulz' article, in which he suggested that usage. The only advantage of using the IFFE version of the closure over the regular version that I can think of is that you don't have to name it (you just have to assign it to a variable in order to call it again)—or if you do name it, it won't be visible in the global scope—so there's one fewer item polluting the global namespace. Is there some other benefit I'm overlooking?
Regular closure:
function createCounter() {
let count = 0;
return function() {
++count;
return `count equals ${count}`;
};
};
let counter = createCounter();
console.log(counter()); // count equals 1
console.log(counter()); // count equals 2
IFFE closure:
let countingFunction = (function() {
let count = 0;
return function() {
++count;
return `count equals ${count}`;
};
})();
console.log(countingFunction()); // count equals 1
console.log(countingFunction()); // count equals 2
Edit: The question marked as a duplicate is a different question, and the chosen answer doesn't answer this question.