Generally, average JavaScript developer tries to create as less global variables as possible.
The main reason of that is to avoid facing too many conflicts, and especially it happens when you deal
with several JavaScript files.
So we need the way that will allow us to write the safe code.
And for that, we can use Effi, which stands for immediately invoked function expression.
If it is a function that runs Asness, it is defined.
OK, let's see what I'm talking about.
You remember that in JavaScript we have function statements.
Function statement means that it has a name and this name is used to invoke function in order to execute
it.
And because of that, functions are first class functions in JavaScript.
We can assign them to variables as a value and this is called function expression.
Okay, let's see what happens when we face to name conflicts so that I'm going to create some variables
on global level and also inside those functions.
So write variable C equals two ten and then again.
Right var C equals to twenty then look variables in castle.
Right.
Consider log C ok then inside function A create again variable C right var sequels and write here from
function eight then return it.
And also create variable see inside function expression, let's copy it and just change A into B and
again, return C.
OK, then at the end, invoke both functions, right, that like a.
And then consider to be.
Reload and we have 20 then from function eight and then from function B..
All right, let's see how these codes are executed.
So when the code runs, at first you know that global execution context is created.
JavaScript engine executes codes line by line, from top to bottom.
It creates memory space for function A, variable B and variable C, which at first has add value 10.
But when the engine hits next line, it doesn't create another variable.
It just overrides 20 to 10.
And then when we run it in council, we got value for variable C.
S 20 after that.
You see that window function eight.
So when the function is run, it creates its local execution context and the block of code inside this
function is executed.
You see, again, we have variable C, which doesn't belong to global execution context.
It's lexically is placed inside function and therefore JavaScript engine creates new variable.
See, the value is not a anymore.
And we get in castle from function eight.
After finishing the execution of function eight, its execution context is gone.
Then the second function is invoked and actually the same happens here.
Local execution context is created for function B, then the new variable C is created and we get getting
counsel from function B..
So as a conclusion, we can say that if we use functions, we can avoid naming conflicts which may be
occured with global variables.
But the better way to write safe code and avoid such naming conflicts is to use immediately invoked
function expressions.
OK, let's go back to Brackett's and remove this code.
First of all, I'm going to write some expression here.
For example, write five plus 10.
If we reload, we won't get any error because it is an expression and when the syntax parser checks,
it recognizes that it is a valid syntax.
But regardless of that, we cannot use this statement.
It doesn't do anything.
But again, it is valid.
OK, let's write anonymous function expression.
You know that anonymous functions do not have names, write function here and also write something inside.
Code braces, for example, of council log.
Hello.
So, as we said, this function doesn't have a name and also it's not assigned to any variable, so
it means that this function as the expression above is not usable.
But when we reload, we will get an error in this case, which tells us that we have something unexpected.
So when Syntex Parser finds the keyword function, that it expects to see the name of this function,
but instead of the name, it finds brace and throws an error.
So this kind of syntax is not belet a like the expression about.
In order to avoid this error, we can wrap this anonymous function with parentheses and if we reload,
then we won't get any error.
OK, in order to execute this function, we need to invoke it.
But you see that it doesn't have a name.
So how can we call this function?
It is very simple.
We just need to use parentheses.
That's reload and you see that it works, function is executed, and we have hello in council, this
is called immediately invoked function expression.
Or sometimes iffy in case of APHIS, we can call them only once.
All right, as usual, we can pass parameters in that function, let's say name.
Also right here, hello, plus name.
And here we can pass as an argument name, for example, John.
Reload and you see that it works.
You notice that we write parentheses outside the braces, it is considered as a better practice, but
you can also call a function inside the parentheses, reload and see that both syntax is valid.
OK, this is very popular and common way to write it is if you check the source code of JavaScript libraries,
for example, Jake worry, you will see that the whole code is wrapped with parentheses.
So using it is a very flexible and great way to write safe code.
OK, besides that, we can use function expression and we can invoke it immediately.
So let's write a variable A equals to function.
Because of that function is assigned to the variable, then Syntex is valid and therefore we don't need
to function with parentheses.
Now, let's invoke the function.
In order to do that, we need to place parentheses after the killer braces.
So let's put some cold inside function, right, consider look, hi.
Reload and we have high in council, if we call again the function using the variable name A, then
we will get an error, which tells us that A is not a function.
The reason of this error is that the function was executed.
And because of that, we do not have here written statement variable A gets the value as undefined.
Let's write here costs look like a.
So you see that the value is undefined, OK, if we return some value, like hello.
The variable, I will get the value, hello.
All right, so in this lecture we have introduced to what ifs are I have demonstrated them using simple
examples, but I'm sure that you noticed how powerful and flexible tool and they could be, as we already
said, immediately invoked function.
Expressions are used in almost every JavaScript library because, again, they allow us to write the
same code.
OK, let's move on.