-: Our next topic is something called,
Immediately Invoked Function Expressions.
So let's take a look at what they are.
So sometimes in JavaScript,
we need a function that is only executed once.
And then never again.
So basically a function
that disappears right after it's called once.
And this might not appear
to make much sense right now.
But we actually need this technique later.
For example, with something called async/await.
So how could we do that?
Well, we could simply create a function.
And then only execute it once.
So, run, once, function.
This will never run again.
And then we can call it here.
However, we can actually run this function again.
At some other point in the code,
if we want it to, right?
There's nothing stopping us,
from later doing run once again, right?
And so this is not what we want to do.
We want to actually execute a function immediately.
And not even having to save it somewhere.
And so this is how we do that.
So we simply write the function expression itself.
So without assigning it to any variable.
Now, if we try to run this,
we will get an error for now.
So it says function statements require a function name.
And that's because
of course JavaScript here expects a function statement.
Because we simply started this line of code here
with the function keyword.
However, we can still trick JavaScript
into thinking that this is just an expression.
And we do that by simply wrapping all
of this into parentheses.
And so now,
we basically transformed the statement that we had before
into an expression.
And so now if we save this,
we get no error.
But also this function didn't execute yet, right?
We never called it.
So we know that this here is the function.
And so, we can then immediately call it.
And so with this,
we will get now this text here locked
to the console, to string.
And indeed here it is, alright?
So again this here,
is really just the function value.
So it's just a function expression.
And then immediately, we call it here.
And so this is why this pattern here,
is called the Immediately Invoked Function Expression.
Or IIFE for short.
So Immediately Invoked Function Expression.
And the same would of course,
also work for an arrow function.
So let's just copy this.
This will also never run again.
So if we try to call it like this,
then it would not work.
And so,
we actually don't get an error.
But also nothing happens.
So we first need to wrap this into parentheses.
And then as we called it.
Then here it is.
Okay, so two ways of writing
an Immediately Invoked Function Expression.
But you might be wondering,
why was this pattern actually invented?
Well, we already know that functions create scopes, right?
And what's important here
is that one scope does not have access
to variables from an inner scope, right?
For example, right here in this global scope.
We do not have access to any variables that
are defined in the scope
of any of these functions here, right?
So for example,
let's add a variable here.
Let's say, is private = 23.
And then as we tried to access it out here.
You already know that it's not going to work, right?
And that's because the scope chain
only works the other way around.
So the inner scope would have access
to anything defined outside,
here in the global scope.
But the other way around,
the global scope does not have access to anything,
that is inside of a scope.
So in this case,
of the scope created by this function here.
Therefore, we say that all data defined
inside a scope is private.
We also say that this data is encapsulated.
So for example, this is private here
is encapsulated inside of this function scope,
that's created here.
And data encapsulation and data privacy
are extremely important concepts in programming.
So many times we actually need to protect our variables,
from being accidentally overwritten by some other parts
of the program.
Or even with external scripts or libraries.
And I'm going to talk in detail about
these concepts in the object oriented programming section.
But for now, keep in mind that it's important
to hide variables.
And that scopes are a good tool for doing this.
And this is also the reason why
The Immediately Invoked Function Expressions were invented.
So basically, this pattern here.
So this is not really a feature, of the JavaScript language.
It's more of a pattern, that some developers came up with.
And that then started to being used,
by many other developers.
Now, do you remember what also creates a scope in ES6?
And that's right.
Variables declared with let or const create
their own scope inside a block.
And we learned that
in the behind the scenes section, remember?
So when we create a block, like this,
and declare is private in there.
Then the outside can still not access is private.
So let's comment out this one,
and paste it here.
And once again,
we cannot access this variable.
while on the other hand,
this one here,
would of course, be accessible.
And so again that's what we learned,
in one of the previous sections.
So that's because this one here was declared with var,
and therefore it does completely ignore
this block here essentially.
And this is the reason why now
in modern JavaScript.
Immediately Invoked Function Expressions are not
that used anymore.
Because if all we want
is to create a new scope for data privacy.
All we need to do,
is to just create a block like this, right?
There's no need to creating a function
to create a new scope.
Unless of course,
we want to use var for our variables.
But we already know,
we probably shouldn't do that. Right?
Now on the other hand,
if what you really need,
is to execute a function just once, then the IIFE.
So the Immediately Invoked Function Expression pattern
is still the way to go.
Even now with modern JavaScript.
And we will actually see a great use case a bit later
of doing an IIFE.