Will const and let make the IIFE pattern unnecessary?

As I understand it, the IIFE pattern is a work around to the fact that ES5 and below do not have a way to create block scopes. By wrapping everything in a function and immediately invoking it, we can create a scope.

Now that let and const will gain support by more the browsers, does this reduce the need for something like the IIFE pattern?


Yes, blocks are going to replace IEFEs, as soon as block-scoped declarations (functions, let/const/class) become widely adopted. You need a scope, e.g. for a closure? Here you have a block, be it a loop body or just part of a statement list.

However, there is still one application of IEFEs that blocks cannot replace: the module pattern. Blocks don't have return values, and mutating higher-scoped variables is ugly, so we will still see function expressions in the creation of objects that need private state:

const example = (() => {
    …
    return …;
}());

Although browser's may begin supporting this, there will always be some random browser that is outdated or is not planning support for this. until it has become standard in all major browsers, it is still recommended that you continue going on with your IIFE pattern until you find it on all majorly used browsers. something you could do is have a script (or google analytics) send information on whether this is undefined or not and until you get at least around 90% of it saying it is not undefined you should continue with IIFE.