Hi there and welcome, this lesson is going to be about JavaScript immediately invoked function expressions
so short for I, I f e and I'm going to be showing you in this lesson what they are and how they work.
So I, Effy, are immediately invoked function expressions.
So this is a JavaScript function that runs as soon as that is defined.
And there are a lot of uses for these types of functions.
It's a design pattern which is also known as self executing anonymous function, and it contains two
major parts.
There's some examples.
Notice that it's also an anonymous function, so there's no name for it.
And with the rounded brackets at the end of the function call, that allows it to immediately invoke
the function.
So there's also another example here where we're setting a variable when it comes to the scope of the
variables so it can contain all of the variables within the scope of the function as it's being invoked.
For example, where we've got first name equals Lowrance.
It can't be seen outside of that function.
There's also when we're declaring a function.
So here we've got result.
We can also immediately invoke that function, returning the first name.
So in this case, the first thing that's going to be returned back is Lowrance.
And you can also posehn Lawrence as a parameter into the function.
When the function is being invoked.
It's going to take that value of Lowrance as first name and it's going to output into the console the
first name value.
You can try this out within your editor.
So there is a challenge for this lesson as well, and that's to output your name in the console using
an immediately invoked function expression.
And there's an example of the code that you can use in order to accomplish this.
So go ahead and pause the video and I'll walk you through the solution to this challenge, as well as
the other expressions that we just looked at.
So what's the function itself?
So they are anonymous functions.
The structure works the same way where we've got a function and then we've got the block.
A code is between the curly brackets.
So the code block is in here.
And then this is what we want to happen within this function.
We can have the welcome output.
And the way that we need to write the functions is we need to wrap it with the rounded brackets.
And this alone, if you notice, that won't do anything yet because we're not invoking the function.
So the last and final part of that function is to invoke it with the rounded brackets.
So just as when you're creating a function and then you invoke the function with the round of brackets,
it works the same way.
And now you can see that that message gets output into the console so we can first name so passing in
a variable and then adding in your name into the parameters.
And then we can use the value that's been passed into the parameters and output that into the console.
So when we refresh it, we get that variable being passed and notice as well.
It works the same way again, as if we were to invoke the function and pass in an argument within this
type of format.
So it works the same way you can pass in multiple arguments.
So all you have to do is comma, separate them and the structure is exactly the same as it was initially.
So we also have the ability to create a function expression.
I'm going to name it result.
And using the immediately invoked function, we're going to return back a value here.
And then this is what we're actually going to see being returned back as a value of result.
So whatever, we're returning back and if we do Hello World and then we console logout the value of
result, you're going to see that because this function is immediately invoking.
And there was one thing that we did miss out here, and that's the rounded brackets to invoke the function.
So we get Hello World.
So this is the same thing as if we did result and we did the invoking of the function down here, we
would get the same result.
And if we missed the rounded brackets, we actually get the function code being returned back.
So you can see that as well within the console.
So if we're not invoking the function, it's not actually running through the code, it's just returning
back all of the code that's associated with that function.
And we do need those rounded brackets in order to invoke the function.
Coming up next is going to be something new with Essex, where we're going to look at aero functions
and this is going to be a short format for writing functions.
So that's still to come.