ES6 Block Scope is The new IIFE

You can probably see where let and const are going to be useful: if you need to scope something to a block, or if you want to make a variable that cannot be changed by accident or on purpose.

Let's take a look at a couple of more examples of when it might be useful.

The first one is replacing the Immediately-Invoked Function Expression, or IIFE. I'm not sure if you've ever heard of this before, but it was coined by Ben Allman back in 2010.

An IIFE function runs itself immediately, and it creates a scope where nothing is going to leak into the parent scope. In our case, nothing is going to leak into the global scope of the window.

If I have a var variable: var name = 'wes'

You can call that in the console, and that's fine here. However, the window already has a name attribute, which is needed when you have a window opening up a another window.

That could something that some third-party JavaScript relies on in order for it to run, or maybe another script is using a variable called name and you accidentally overwrite that. It can get a little bit messy.

The way the IIFE fixes that is that the function runs immediately and you put your variables inside of that:

(function() {
var name = 'wes';
})();

These variables are now scoped to the IIFE function, and because var variables are function-scoped, they are not available in the global scope.

If you try to call name in the console now, it's not undefined, it's blank because, like I mentioned, it's just blank because that is a property that lives on the window natively in JavaScript.

If I needed to access our function's name, obviously, I'd have to do a console.log inside of the IIFE function, but the important thing is that it's no longer leaking into the global scope.

With let and const variables, we don't need a function for our variables to be scoped to that.

Why? Because let and const use block scope.

Let's start over with a const instead of a var

const name = 'wes';

If we call this in the console, we'll see 'wes', but if we wrap it in curly brackets:

{
const name = 'wes';
}

Our const is going to be scoped to that block. If you try to call name in the console, we'll get the window's name, which is blank. But if we add a console.log to our block:

{
const name = 'wes';
console.log(name);
}

...we'll get wes in the console. You don't the IIFE stuff anymore. You're using let and const because they are going to be scoped to that block.

Find an issue with this post? Think you could clarify, update or add something?

All my posts are available to edit on Github. Any fix, little or small, is appreciated!

Edit on Github

Syntax Podcast

Hold on — I'm grabbin' the last one.

Listen Now →
Syntax Podcast

@wesbos Instant Grams

Master Gatsby

Master Gatsby

Building modern websites is tough. Preloading, routing, compression, critical CSS, caching, scaling and bundlers all make for blazing fast websites, but extra development and tooling get in the way.

Gatsby is a React.js framework that does it all for you. This course will teach you how to build your websites and let Gatsby take care of all the Hard Stuff™.

MasterGatsby.com
I post videos on and code on

Wes Bos © 1999 — 2021