This is for sure, a question that they hear on every single JavaScript interview.
This is why you must understand border closures.
As you can see here, we have a task create a counter function which has increment and get value functionality.
And actually, you might not get it from this task, but this is a task to guide enclosures because
we must create a function where inside with some value and we have two public methods and this is how
you typically need to implement it.
So we want to create our, for example, private account to function.
And this is private because with certain accounting side, which is not available outside.
And actually, this is just a function.
But if you hear an interview that you must or something inside function after you call this function,
then you have a question regarding closure.
So what we want to do here, we want for us to create our counter.
So let's name it combed, and by default, it will be zero.
Melvin must return our public pay.
This two methods increment and get fairly functionality.
This is why we are making here return and inside we want to return to things.
First of all, it is an increment function and this is just a property where we are passing inside our
value and what we want to do.
We want to change this count that we have on the top here.
So we are right in here, count plus equal and here is our value that we want to increase.
And actually, it might be that we want to increase it by default, just by one.
This is why it makes sense to pass here.
One In this case, we can simply call increment and it will work out of the box and the next method
will be here.
Get value because we want to get our count back and actually here.
This is just a function which will return our count.
And the main idea is that this is our public pay.
This is what is available inside this function after we are calling it.
Let's check this out.
Let's create here our counter and work call and here our private account function.
And actually, what they want to do here.
I want to console.log.
First of all, our counter don't get value.
This is our method to get our counter and second level one to call counter dot.
And here is increment to increment our value and then counter once again to check value once again.
Let's check if it's working.
I am reloading the page.
We're getting zero and one.
But the most interest in the idea of what we have inside this private counter, if we are right here,
private counter, you can see that this is a function and actually we don't have any access to this
property comp.
So this property count in this case is private.
And even if I am right in here counter, you can see that we're getting an object with two values get
value and increment, and we don't have any access to our counter property inside.
So this code is working, but it is really tricky and complex because this is a closure.
What does closure ever mean?
Closure means that we have access inside of a function to the outside scope, which actually means here,
for example, inside increment by using count, and we don't have a discount inside this function.
We have it outside here on the top and exactly the same regard and get value.
We have this counter here on the top, not inside this function, which actually means closure function
has access to all properties inside itself and all properties which were defined default.
Why it is important because actually with this single line counter equals private counter work, creating
a closure because by now account counter exist.
We have a closure and we have this reference to this counter.
This is why every single time one of our colon get value and increment, it is working and it is referenced
in this count on the top.
And you might say okay, but it is sounding like magic.
I don't understand how it is looking like in JavaScript, and this is where console directory can help
us.
If people write inside console not console.log, but console directory and then say they want to pass
a function, in our case, a discount to get value and then hidden here and here we don't see a body,
but instead our function is an object and we're interested here in part, which is called scopes.
And this is exactly what we're interested about after we open here scopes, we have here a property
closure and this is exactly our closure.
As you can see here, we have closure, private counter and here count equals one, which means exactly
we have this.
Let's count equals zero and now it is inside closure.
This is why it is available first inside JavaScript and all this questions regarding closures are exactly
the same.
For example, you might be asked the question you need to create a function which will store inside
six Austrian and will just return it one of a call.
This function again, so it will look something like this.
For example, let's create the function private secret.
And here we don't pass anything inside, but we want to do inside this create our secret sin.
So let's just create a secret and it equals fool.
So the secret is exactly our closure, and for it to be available outside, we must return here a function.
So with simply return here, a function and.
This function will return this secret.
This means that when we are creating outside a function which we can name, for example, get secret,
we must call here our private secret function.
The main idea is that private secret function gives back a function.
In this case, our secret is a function.
And now we can console.log our get secret and we just call it.
And actually, as you can see in console, we're getting this full strain back.
This is why our secret screen is actually a closure and it is available outside after restore this private
secret inside.
Additional property in this case were startling disclosure after our function was executed and closure
is a super popular pattern inside JavaScript.
This is why you must know how to use closures.