0% found this document useful (0 votes)
2 views8 pages

Closures In JavaScript

This document explains the concept of closures in JavaScript, highlighting how functions can access variables from their outer scope. It provides an example with nested functions to illustrate how an inner function retains access to the variables of its outer function even after the outer function has finished executing. The conclusion emphasizes that closures allow functions to maintain access to their parent scope beyond its lifecycle.

Uploaded by

Ibrahim Sabo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Closures In JavaScript

This document explains the concept of closures in JavaScript, highlighting how functions can access variables from their outer scope. It provides an example with nested functions to illustrate how an inner function retains access to the variables of its outer function even after the outer function has finished executing. The conclusion emphasizes that closures allow functions to maintain access to their parent scope beyond its lifecycle.

Uploaded by

Ibrahim Sabo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
HUST VES Ta > 8 Hi EVERYONE! In Today's post, we will see the concept of closures in Javascript. Closures are one of the easiest concepts of Javascript. Don't forget to save this post so you can access it whenever you & needed. “s Do You know In many languages, you can't use or access the global variables inside the function. But In Javascript, a function has the access to all the variables that have been declared in the higher scope or outside the function. And this feature is called as Closure. Closure in Nested Functions As we have seen that a function can access the global variables or the variables outside of it. so in a similar manner suppose if there are two functions say innerFunction and outerFunction and innerFunction is inside the outerFunction. So the innerFunction will have the access to the variables and parameters of the outerFunction. This is the most common use of closures. Example: Siac ie-iaat eae Den S let b = 10; return function innerFunction( ){ AUS A bo console.log("The sum is of a and b is: " + sum); ip + let result = outerFunction(5); result()3 II! Attention Please: In the above example, | made two functions(outerFunction and innerFunction). innerFunction is inside the outerFunction. a is the parameter and b is the variable of the outerFunction but i can access it inside the innerFunction. This is known as closures in nested functions but (see the next slide...) aCe VM Maey ae M ola dial eg The outerFunction is returning the innerFunction ica la Maen ena Te selena eel e let sum = atb; console.log("The sum is of a and b is: " + sum); yi The innerFunction taking the values of a and b from outerFunction Sia aeo oie a Knee D iat let b = 10; return function innerFunction( ){ ACh ee oy console.log("The sum is of a and b is: " + sum); Then | called the outerFunction and give it the argument "5" and stored that call in the variable result. let result = outerFunction(5); Actually what happened here is: The outerFunction returned or gave me the innerFunction and result variable holds that innerFunction. and due to this you can say that: result variable also becomes the function because it holds the innerfunction. Then i called the result. result(); So even after the outerFunction stopped executing. (here it stopped its Tol idlo) a) let result = outerFunction(5); NN (ar-Taoesi 4 -xciau ayaa gl MOU lvoLUlmni mean ma alc inner function. The sum is of a and b is: Conclusion: A closure is a function having access to the parent scope, even after the parent function has closed.

You might also like