What is Closure in JavaScript
In this article, we will learn another most important concepts of javascript, which is closure.
What is Closure in Javascript?
A closure is the combination of a function and the lexical environment within which that function was declared.
When a function is a closure, it continues to have access to the scope of its outer functions even after the outer function has returned.
This means that even after a function has been completed, a closure can remember and access variables and arguments from the outer function.
In Simple words, closure is that is inner function can have access to the outer function variables/parameters as well as all the global variables.
For example,
const outerFun = (a) => {
let b = "Jigar";
const innerFun = () => {
console.log(a + b);
};
return innerFun;
};
let inner = outerFun("Hello..");
inner();