In JavaScript, closures are a powerful and often misunderstood concept. They play a crucial role in maintaining data privacy, enabling functional programming patterns, and facilitating the creation of robust and modular code. In this article, we'll explore closures in-depth through a practical example.
Introduction to Closures:
A closure is formed when a function retains access to its lexical scope even after the outer function has finished executing. This means that the inner function has access to variables and parameters of the outer function, even after the outer function has returned. Closures are a fundamental feature of JavaScript and are commonly used in various programming paradigms.
Regarding this definition, don't panic. I can simplify it:) 😀
Example: Exploring Closure with Lexical Scoping
Let's delve into a simple example to understand closures better:
function display() {
let name = "Manikandan";
function printName() {
console.log(`Hi, This is ${name}.`)
}
return printName;
}
let res = display(); // [Function: printName]
res(); // Hi, This is Manikandan.
Understanding the Example:
-
Function Declaration (
display):- Inside the
displayfunction, a variablenameis declared and assigned the value"Manikandan". - Another nested function
printNameis declared withindisplay. This function accesses the variablenamefrom its lexical scope.
- Inside the
-
Returning a Function:
- The
displayfunction returns theprintNamefunction.
- The
-
Function Execution:
-
display()is called and its result is stored inres. -
resnow holds a reference to theprintNamefunction.
-
-
Closure in Action:
- When
res()is invoked, it executes theprintNamefunction. - Despite
displayalready finished executing, theprintNamefunction still retains access to thenamevariable due to closure. - Thus, it successfully logs
Hi, This is Manikandan.to the console.
- When
Explanation of Lexical Scoping:
- Lexical scoping means that a function's scope is determined by its surrounding lexical context at the time of its definition.
- In our example,
printNameis defined within the lexical scope ofdisplay. Therefore, it has access to the variables defined withindisplay, includingname. - Even though
displayfinishes executing beforeprintNameis called,printNamemaintains a reference to thenamevariable through closure, allowing it to access and use the variable's value.
Nested Function
Absolutely! In JavaScript, closures extend to nested functions as well, allowing them to access variables from their containing scopes even if those scopes have finished executing.
function display() {
let name = "Manikandan";
function printNaame() {
let age = 25;
function printAge() {
console.log(`Hi this is ${name}, And My age is ${age}`)
}
return printAge;
}
return printNaame;
}
let res = display(); // [Function: printNaame]
let result = res(); // [Function: printAge]
result();
Conclusion:
Now Closure is,
Inner function/nested function, whether it returns or not, will always have access to the outer scope variable. This is the concept of closures.
Top comments (0)