The Power of Closures in JavaScript.

Introduction
In the fast-paced world of JavaScript, knowing the idea of closures is similar to discovering a hidden treasure vault of programming skill. Closures, an essential component of JavaScript's lexical scope environment, are the key to efficient code structuring and sophisticated functionality.
Learning Lexical Scopes and Closures in JavaScript.
JavaScript's lexical scope environment opens the way for a unique approach for variable access within functions. When a function seeks a variable, it searches its local memory. If the variable cannot be found, the function searches the lexical parent's memory. This complex procedure produces closures, as shown in the code sample below.
function x() {
var a = 7;
function y() {
console.log(a);
}
return y;
}
var z = x();
console.log(z); // The value of z is the entire code of function y.
This code not only returns the function y, but also encapsulates the whole closure, including function y and its lexical scope, in the variable z. As a result, when z is used elsewhere in the code, it keeps the memory of var a within x().
Studying Closure with Another Example.
To further understand the notion, consider the following example:
function z() {
var b = 900;
function x() {
var a = 7;
function y() {
console.log(a, b);
}
y();
}
x();
}
z();
// Outputs:
7
900
In simple terms, a closure is a function that has access to its outside function scope even after the function has completed. This inherent characteristic enables closures to retrieve and access variables and argument references from their outer function even after the function has returned.

Advantages of Closures:
Module Design Pattern.
Currying.
Memorization.
Data Hiding and Encapsulation.
setTimeouts, etc.
Disadvantages of closures:
excessive memory consumption.
Possible issues include memory leaks and browser freezing.





