Explain Closure in detail ?
December 23, 2024

Explain Closure in detail ?

Closures are features in JavaScript where a function can “remember” its variables lexical Scope(the scope in which it was created) even after the outer function has finished executing. This allows the inner function to access variables from its enclosing scope even if the outer function is no longer active.


Closure example

function outerFunction(outerVariable){
  return function innerFunction(innerVariable){
     console.log(`outer variable : ${outerVariable}`);
     console.log(`inner variable : ${innerVariable}`);

};}

const closureFunction = outerFunction('outside');
closureFunction('inside');
Enter full screen mode

Exit full screen mode

2024-12-23 06:15:08

Leave a Reply

Your email address will not be published. Required fields are marked *